0

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?

3
  • 2
    That's invalid JSON, bru. Commented Jun 19, 2013 at 21:50
  • i think this might help { id: 2011136021, tree_level: 3, main_category_id: 105, sub_nodes: [ 128001, 128002, 128003, 2011136046 ],}, Commented Jun 19, 2013 at 21:54
  • I think this can help : [{"id":2011136021,"tree_level":3,"main_category_id":105,"sub_nodes":[128001,128002,128003,2011136046],"attributes":{"@id":"2011136021","@uri":"/api/metadata/category/2011136021","level":"SHOW","levelTypeId":6,"parent":{"@id":"126","@uri":"/api/web/category/126","articles":"","metadata":{"@uri":"/api/metadata/category/126"},"categories":{"@uri":"/api/web/category/126/categories"},"available":false},"parentId":126,"priority":1,"properties":"","title":"Moods of Norway" Commented Jun 19, 2013 at 22:53

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

it says "can't convert Array into String" any idea?
Your JSON is invalid. Look at the JSON in my answer. Just, never mind.
This is what i have got, i used JSONVIEW chrome extension { id: 2011136021, tree_level: 3, main_category_id: 105, sub_nodes: [ 128001, 128002, 128003, 2011136046 ], attributes: {} },]
Right, and what you just pasted is not valid JSON. Read the JSON spec for more details.
is this the correct format you are looking for? [{"id":2011136021,"tree_level":3,"main_category_id":105,"sub_nodes":[128001,128002,128003,2011136046],"attributes":{"@id":"2011136021","@uri":"/api/metadata/category/2011136021","level":"SHOW","levelTypeId":6,"parent":{"@id":"126","@uri":"/api/web/category/126","articles":"","metadata":{"@uri":"/api/metadata/category/126"},"categories":{"@uri":"/api/web/category/126/categories"},"available":false},"parentId":126,"priority":1,"properties":"","title":"Moods of Norway"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.