How to Create Pretty JSON in Chef (Ruby)

How do you create pretty json in CHEF (ruby)

As pointed out by this SO Answer .erb templates are great for HTML, and XML, but is not good for json.

Luckily, CHEF uses its own json library which has support for this using .to_json_pretty

@coderanger in IRC, pointed out that you can use this library right inside the recipe. This article shows more extensively how to use chef helpers in recipes.

default.rb

# if ['foo']['bar'] is null, to_json_pretty() will error
default['foo']['bar'] = {}

recipe/foo.rb

pretty_settings = Chef::JSONCompat.to_json_pretty(node['foo']['bar'])

template "foo.json" do
source 'foo.json.erb'
variables :settings => pretty_settings
action :create
end

Or more concise as pointed out by YMMV

default.rb

# if ['foo']['bar'] is null, to_json_pretty() will error
default['foo']['bar'] = {}

recipe/foo.rb

template "foo.json" do
source 'foo.json.erb'
variables :settings => node['foo']['bar']
action :create
end

templates/foo.json.erb

<%= Chef::JSONCompat.to_json_pretty(@settings) %>

How to pretty format JSON output in Ruby on Rails

Use the pretty_generate() function, built into later versions of JSON. For example:

require 'json'
my_object = { :array => [1, 2, 3, { :sample => "hash"} ], :foo => "bar" }
puts JSON.pretty_generate(my_object)

Which gets you:

{
"array": [
1,
2,
3,
{
"sample": "hash"
}
],
"foo": "bar"
}

How to convert ruby dsl chef environment file to JSON format?

You can do it the same way Chef does :)

https://github.com/chef/chef/blob/master/lib/chef/environment.rb

require 'chef'
Chef::Config[:environment_path] = '/path/to/directory/with/rb_file'
env = Chef::Environmment.load_from_file('environment_name') # ! => not filename!
env.to_json

How to pass an additional json file to chef client

Your two options are either make it into node attributes (through -j or making it in to a role) or make it into a data bag item by placing it at ./data_bags/whatever/itemname.json.

Chef Solo - Role Data in Ruby DSL or JSON?

I prefer to use JSON format wherever possible for one simple reason - it's easy to parse and validate with a script. Here are three things that you can do if all your Chef data is in JSON format:

  1. Easily perform a syntax check in a git pre-commit hook, something that's much harder to do when the file is in the Ruby DSL format.
  2. Validate the keys and values in a data bag entry. This can be useful to check that you are not going to deploy invalid or nonsensical data bag entries to production.
  3. Compare (with a little extra work - key ordering in a dictionary needs to be taken into account) the value of an object on a server with what's in git. The --format json argument is useful here.

How to make Ruby JSON parser ignore json_class?

I just had the same problem, and found the answer by reading the source of the JSON gem - just unset JSON.create_id before trying to do the parse:

JSON.create_id = nil
JSON.parse('{ "json_class": "Chef::Role" }').class => Hash

EDIT: Note that since version 1.7 of the gem (1.8.0 is current as I type this update), the above hack is no longer necessary. JSON#parse now ignores json_class, and JSON#load should be used instead for unmarshalling dumped objects.

How to handle optional methods/attributes of json in Chef

The .foo syntax for attributes has been removed in newer Chef, so we don't recommend you use it all. Instead do <%= user["name"] %> and then for something that needs a default <%= user["role"] || "defaultvalue" %>.



Related Topics



Leave a reply



Submit