Puppet Function: jsonreplace
- Defined in:
- lib/puppet/parser/functions/jsonreplace.rb
- Function type:
- Ruby 3.x API
Overview
Reads a JSON file, replaces the given key value for the supplied value and returns the resulting JSON as a string.
For example:
$test = jsonreplace($file, "somekey", "somevalue")
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/puppet/parser/functions/jsonreplace.rb', line 5 newfunction(:jsonreplace, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args| Reads a JSON file, replaces the given key value for the supplied value and returns the resulting JSON as a string. For example: $test = jsonreplace($file, "somekey", "somevalue") ENDHEREDOC unless args.length == 3 raise Puppet::ParseError, ("hash2props(): wrong number of arguments (#{args.length}; must be 3)") end if File.exists?(args[0]) json = File.read(args[0]) jsonp = JSON.parse(json) jsonp[args[1]] = args[2] JSON.pretty_generate(jsonp) else "{}" end end |