0

lets say I have this json data file

{
  "page": {
    "title": "Example Page"
  },
  "employers": {
    "name": "Jon"
  },
  "employees": [
    { "name": "Mike", "nicknames": ["Superman"] },
    { "name": "Peter", "nicknames": ["Peet", "Peetee", "Peterr"] }
  ]
}

this data.json file exist as a file outside of the script

I have these 3 lines to read and parse it with json ruby library

data = File.read("data.json")
obj = JSON.parse(data)
puts obj.values

in my terminal it comes out to be like this

{"title"=>"Example Page"}
{"name"=>"Jon"}
{"name"=>"Mike", "nicknames"=>["Superman"]}
{"name"=>"Peter", "nicknames"=>["Peet", "Peetee", "Peterr"]}

what happened to employers and employees? now I have the same key or name in this case. Its difficult for me to grab the values to use them.

1
  • what data exactly do you need from this json? Commented Sep 30, 2014 at 21:23

1 Answer 1

4

Employers and employees are the keys for primary hash, and you requested values, that's why you get what you get. Try putting obj .

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

Comments

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.