Puppet Function: to_json

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

Summary

Convert a data structure and output to JSON

Overview

to_json(Any $data)String

Examples:

Output JSON to a file

file { '/tmp/my.json':
  ensure  => file,
  content => to_json($myhash),
}

Parameters:

  • data (Any)

    Data structure which needs to be converted into JSON

Returns:

  • (String)

    Converted data to JSON



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'vendor_modules/stdlib/lib/puppet/functions/to_json.rb', line 6

Puppet::Functions.create_function(:to_json) do
  # @param data
  #   Data structure which needs to be converted into JSON
  #
  # @example Output JSON to a file
  #   file { '/tmp/my.json':
  #     ensure  => file,
  #     content => to_json($myhash),
  #   }
  #
  # @return [String] Converted data to JSON
  dispatch :to_json do
    param 'Any', :data
  end

  def to_json(data)
    data.to_json
  end
end