Help addressing field values from JSON

Hi all,
I’ve tried to read values from a json file without success.

The following code works well:

require ‘json’
require ‘pp’
brut = File.read(‘repsol.json’)
dades = JSON.parse(brut)

p dades[‘list’][“resources”] ----> returns data but too general
p dades[‘list’][‘resources’][‘resource’] —> fails
p dades[‘list’][‘resources’][‘price’] —> fails

Pls, can anyone help me with ruby hash syntyax to address for example
the ‘price’ value?
(file content provided below)
Any help or tip will be apreciated

Thanks in advance!
Jordi


By the way, the repsol.json file follows:

{
“list” : {
“meta” : {
“type” : “resource-list”,
“start” : 0,
“count” : 2
},
“resources” : [
{
“resource” : {
“classname” : “Quote”,
“fields” : {
“name” : "ENDESA ",
“price” : “17.594999”,
“symbol” : “ELE.MC”,
“ts” : “1461080280”,
“type” : “equity”,
“utctime” : “2016-04-19T15:38:00+0000”,
“volume” : “1123718”
}
}
}
,
{
“resource” : {
“classname” : “Quote”,
“fields” : {
“name” : “REPSOL”,
“price” : “10.915000”,
“symbol” : “REP.MC”,
“ts” : “1461080280”,
“type” : “equity”,
“utctime” : “2016-04-19T15:38:00+0000”,
“volume” : “10877532”
}
}
}

]
}
}

Take a good look at the JSON structure: ‘resources’ is an array, you can
access its entry with “…[index]”

p dades[‘list’][‘resources’][0][‘resource’][‘fields’][‘price’]

or

p dades[‘list’][‘resources’][1][‘resource’][‘fields’][‘price’]

Dansei,
You are the king!
Many many thanks for your quick answer. It works (of course).

After some hours os googling i haven’t been able to see it.

Best regards
Jordi

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs