Puppet Function: hash2json

Defined in:
lib/puppet/parser/functions/hash2json.rb
Function type:
Ruby 3.x API

Overview

hash2json()Any

Accepts a hash and returns its JSON representation.

For example:

$test = hash2json($hash)

Returns:

  • (Any)


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/puppet/parser/functions/hash2json.rb', line 2

newfunction(:hash2json, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
  Accepts a hash and returns its JSON representation.

  For example:

      $test = hash2json($hash)

  ENDHEREDOC

  unless args.length == 1
    raise Puppet::ParseError, ("hash2json(): wrong number of arguments (#{args.length}; must be 1)")
  end
  
  unless args[0].empty?
    PSON.pretty_generate(args[0])
  else
    "{}"
  end
end