3 # Convert data structure and output to pretty JSON
6 # * how to output pretty JSON to file
7 # file { '/tmp/my.json':
9 # content => to_json_pretty($myhash),
12 # * how to output pretty JSON skipping over keys with undef values
13 # file { '/tmp/my.json':
15 # content => to_json_pretty({
16 # param_one => 'value',
20 Puppet::Functions.create_function(:to_json_pretty) do
22 # data structure which needs to be converted to pretty json
24 # value `true` or `false`
26 # converted data to pretty json
27 dispatch :to_json_pretty do
28 param 'Variant[Hash, Array]', :data
29 optional_param 'Boolean', :skip_undef
32 def to_json_pretty(data, skip_undef = false)
35 data = data.reject { |value| value.nil? }
37 data = data.reject { |_, value| value.nil? }
40 JSON.pretty_generate(data) << "\n"