Embedding JSON Data into YAML file Embedding JSON Data into YAML file ruby ruby

Embedding JSON Data into YAML file


I believe taking it into quotes should do the trick:

portslist: '[{"name":"ob1","port_type" ... }]'


clarkevans' comment on the accepted answer suggested a better answer for long bits of JSON, because you can wrap the lines. I looked up the block scalar syntax he mentioned, and thought I would include an example here:

portslist: >  [{"name":"ob1","port_num":0,"port_type":"network"},  {"name":"ob2","port_nu...


If you have the string, you can use as simple as Vlad Khomich mentioned:

portslist: '[{"name":"ob1","port_num":0,"port_type":"network"},...]'

If you are using ERB and have an object, you can use to_json and inspect to escape to a JSON string:

portslist: <%= [{name: 'ob1', port_num: 0, port_type: 'network'},...].to_json.inspect %>

And if you have a large JSON specification, you can store it in a separated file and load using Ruby, so you can keep your YAML file clean:

portslist: <%= File.read('/path/to/file.json').inspect %>