1 # Take a data structure and output it as pretty JSON
3 # @example how to output pretty JSON
4 # # output pretty json to a file
5 # file { '/tmp/my.json':
7 # content => to_json_pretty($myhash),
10 # @example how to output pretty JSON skipping over keys with undef values
11 # # output pretty JSON to a file skipping over undef values
12 # file { '/tmp/my.json':
14 # content => to_json_pretty({
15 # param_one => 'value',
22 Puppet::Functions.create_function(:to_json_pretty) do
23 dispatch :to_json_pretty do
24 param 'Variant[Hash, Array]', :data
25 optional_param 'Boolean', :skip_undef
28 def to_json_pretty(data, skip_undef = false)
31 data = data.reject { |value| value.nil? }
33 data = data.reject { |_, value| value.nil? }
36 JSON.pretty_generate(data) << "\n"