0

I am having trouble accessing parts of a json that looks like this. The thing I am looking to get is precip type.

{
        "latitude": 42.3601,
        "daily": {
          "data": [
            {
              "time": 255589200,
              "icon": "snow",
              "sunriseTime": 255613996,
              "sunsetTime": 255650764,
              "moonPhase": 0.97,
              "precipIntensity": 0.0354,
              "precipIntensityMax": 0.1731,
              "precipIntensityMaxTime": 255657600,
              "precipProbability": 1,
              "precipAccumulation": 7.337,
              "precipType": "snow",
              "temperatureHigh": 31.84,
            }
          ]
        },
        "offset": -5
      }

So far I have tried

response['daily']['data']['precipType']

and also this (I didn't expect this to work though, it was just a try)

response['daily']['data.precipType']

1 Answer 1

3

Since data is an array you need to access the items inside the array by the index.

puts response["daily"]["data"][0]["precipType"]

For the first element in the array.

Or loop over all items in the array like so

response["daily"]["data"].each do |item|
  puts item["precipType"]
end
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the feedback.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.