Reputation: 13
I have a JSON file formated like this:
[
{
id: 2011136021,
tree_level: 3,
main_category_id: 105,
sub_nodes: [
128001,
128002,
128003,
2011136046
],
}
]
I want to return the value of sub_nodes
based on the parent ID 2011136046:
sub_nodes: [
128001,
128002,
128003,
2011136046
],
Can someone please help me with that?
Upvotes: 0
Views: 61
Reputation: 135197
For just one second, let's pretend you have valid JSON
require "json"
json = '{"id":2011136021,"tree_level":3,"main_category_id":105,"sub_nodes":[128001,128002,128003,2011136046]}'
obj = JSON.parse(json)
# => {"id"=>2011136021, "tree_level"=>3, "main_category_id"=>105, "sub_nodes"=>[128001, 128002, 128003, 2011136046]}
sub_nodes = obj["sub_nodes"]
# => [128001, 128002, 128003, 2011136046]
If you want to convert sub_nodes
back to JSON
JSON.generate(sub_nodes)
# => "[128001,128002,128003,2011136046]"
But now we can stop pretending since you don't have valid JSON.
Upvotes: 3