Jngai1297
Jngai1297

Reputation: 2495

using ruby json library to parse json data into a hash some keys missing

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.

Upvotes: 0

Views: 114

Answers (1)

Yaro
Yaro

Reputation: 568

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

Upvotes: 4

Related Questions