Create json object from string with ruby

I am really new to Ruby development. I am trying to create a json file with strings. My json file is as below. Can you help me?

{ "App":{ "properties":{"color":"red"}, "screens":[ {"id":"page1", "properties":{"color":"red"}, "elements":[ {"type":"txtbox", "properties":{"color":"red"}}, {"type":"button", "properties":{"color":"red"}} ] }, {"id":"page2", "properties":{"color":"red"}, "elements":[ {"type":"txtbox", "properties":{"color":"red"}}, {"type":"button", "properties":{"color":"red"}} ] } ] } } 
+6
source share
1 answer

You can parse JSON with ruby ​​from the hash:

 require 'json' my_hash = JSON.parse('{"hello": "goodbye"}') puts my_hash["hello"] => "goodbye" 

Or generate it from the hash:

 require 'json' my_hash = {:hello => "goodbye"} puts JSON.generate(my_hash) => "{\"hello\":\"goodbye\"}" 

Check out the JSON documentation .

+15
source

Source: https://habr.com/ru/post/985576/


All Articles