Embedding JSON Data into YAML file

后端 未结 5 1971
孤城傲影
孤城傲影 2020-12-13 16:52

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:



        
5条回答
  •  囚心锁ツ
    2020-12-13 17:59

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

提交回复
热议问题