I am trying to create JSON in Ruby from data coming from a SQL Server table, based off a query. I've worked with Ruby quite a bit and JSON some. But never together.
This is a sample of the JSON I'm trying to create.
Even help with just creating the JSON with the nested arrays and the root element would be helpful.
{
"aaSequences": [
{
"authorIds": [
"ent_fdfdfdfdf_one"
],
"aminoAcids": "aminoAcids_data",
"name": "bbbbb-22",
"schemaId": "ls_jgjgjg",
"registryId": "src_fgfgfgf",
"namingStrategy": "NEW_IDS"
},
{
"authorIds": [
"ent_fdfdfdfdf_two"
],
"aminoAcids": "aminoAcids_data",
"name": "bbbbb-22",
"schemaId": "ls_jgjgjg",
"registryId": "src_fgfgfgf",
"namingStrategy": "NEW_IDS"
}
]
}
CodePudding user response:
Generate a JSON from a Ruby hash object
To generate a json, first start with a hash (like a dict) in Ruby
my_hash = {:foo => 1, :bar => 2, :baz => 3}
Make sure you require the json package as well
require 'json'
Then you can simply convert the hash object to a JSON string
my_hash.to_json # outputs: "{'foo': 1, 'bar': 2, 'baz': 3'}"
You can nest arrays into your hash as well
my_hash_2 = {:foo => [1, 2, 3, 4], :bar => ['a', 'b', 'c', 'd']}
I'll let you try that one on your own, but ruby will handle the nested object just fine for you.