Embedding JSON Data into YAML file

76,593

Solution 1

I believe taking it into quotes should do the trick:

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

Solution 2

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...

Solution 3

The | is also possible. For example.

MyObject:
  type: object
  example: |
    {
        "id": 54,
        "manufacturer": "ACME",
        "location": "New York",
        "createdAt": "2012-10-01 07:42:35.825565",
        "description": "test",
    }

Solution 4

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 %>

Solution 5

For the sake of being complete: In case you're using ActiveRecord::Store, you can load your data simply using YAML representation of the same data, even if it is a JSON store:

one:
  portslist:
    - 
      name: 'ob1'
      port_num: 0
      port_type: 'network'
    - 
      name: 'ob2'
      port_num: 1
      port_type: 'network'
Share:
76,593
Saurajeet
Author by

Saurajeet

Updated on October 01, 2021

Comments

  • Saurajeet
    Saurajeet over 2 years

    I am writing a fixture for my table. And a one of the coloums takes in a JSON string as a value.

    The problem is the fixture is not loading failing as:

    Fixture::FormatError: a YAML error occurred parsing /home/saurajeet/code/dcbox/test/fixtures/hardware.yml. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.html
    The exact error was:
      ArgumentError: syntax error on line 145, col 73: `  portslist: [{"name":"ob1","port_num":0,"port_type":"network"},{"name":"ob2","port_nu'.....
    

    Any solutions to this.

  • clarkevans
    clarkevans about 12 years
    This makes the value be a string, so it's not really embedding. If you want to do this, I'd use a block scalar and indent.
  • Thermatix
    Thermatix over 9 years
    Strings are scalar type, but I get what you mean.
  • kamal
    kamal over 5 years
    play.golang.org/p/XkVEeVqakNv I am trying with this, but not working for me
  • Admin
    Admin almost 5 years
    yeah ok, what about multi-line JSON?
  • athavan kanapuli
    athavan kanapuli over 4 years
    This works. But is there a way in YAML where I can reference an external JSON file and set it as a value inside the YAML ?
  • Tejas Mehta
    Tejas Mehta over 3 years
    Any one has idea how to reference an external JSON file inside yaml file. Also, above internal JSON is working for me.