damoiser
damoiser

Reputation: 6238

managing complex JSON

I have a complex input JSON, something like this:

@json = {"project":"bla",
    "analysis": {"id":"123","title":"Test"}, 
    "data":{"axis": {"name":"column", "label":"demo","values": ["one", "two"]},
           "series": [{"label":"text", "values":["1", "2", "3"]}, 
                    {"label":"text2", "values":["4", "5", "6"]}
]}}

(more complex and length that this example)

two question:

  1. it exists a RoR method that can delete on the fly something in the json? something like @json.destroy['analysis'] that eliminate only the "analysis" key-value couple?

  2. how can i navigate (for example) in the values of the series? I can do if the json is simple with a do each, but here must I do a concecation of do each?

Upvotes: 1

Views: 300

Answers (1)

user1454117
user1454117

Reputation:

You can convert the JSON to a hash, remove the key, and then convert back to JSON:

require "json"
hash = JSON.parse(@json)
hash.delete("analysis")
@json = hash.to_json

Upvotes: 1

Related Questions