Puppet Function: to_json_pretty

Defined in:
modules/stdlib/lib/puppet/functions/to_json_pretty.rb
Function type:
Ruby 4.x API

Overview

to_json_pretty(Variant[Hash, Array] $data, Optional[Boolean] $skip_undef)Any

Parameters:

  • data (Variant[Hash, Array])
  • skip_undef (Optional[Boolean])

Returns:

  • (Any)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'modules/stdlib/lib/puppet/functions/to_json_pretty.rb', line 22

Puppet::Functions.create_function(:to_json_pretty) do
  dispatch :to_json_pretty do
    param 'Variant[Hash, Array]', :data
    optional_param 'Boolean', :skip_undef
  end

  def to_json_pretty(data, skip_undef = false)
    if skip_undef
      if data.is_a? Array
        data = data.reject { |value| value.nil? }
      elsif data.is_a? Hash
        data = data.reject { |_, value| value.nil? }
      end
    end
    JSON.pretty_generate(data) << "\n"
  end
end