1

I'm trying to parse the below from JSON (using the JSON gem) in Ruby:

{ 
  "daily": {
    "summary":"Light rain today through Saturday, with temperatures rising to 88°F on Saturday.",
    "icon":"rain",
    "data":[
      {
        "time":1435464000,
        "precipProbability":0.99
      }
    ]
  }
}

Currently what I have is this: forecast["daily"]["data"], but I want to get the precipProbability for time when it is 1435464000. Any suggestion on how to complete my current JSON parsing query?

4
  • 1
    you could write forecast["daily"]["data"].first['precipProbability'] Commented Jun 28, 2015 at 13:23
  • ^ If you can submit as an answer, I will mark this as correct. Thanks! :) Commented Jun 28, 2015 at 16:03
  • 1
    @Ojash your answer works with the example provided but if anymore elements are added to that array you can't be sure that you'll get the element for the time the OP is looking for. Commented Jun 28, 2015 at 16:08
  • very true @LeoCorrea, i'd rather loop it out and display, since its an array you never know ;) and i am pretty much sure that maclover7 didnt mean to query exact same time, my assumption is it may vary, whats needed is the precipitation probability. Commented Jun 28, 2015 at 18:09

4 Answers 4

2

Since the data field is an array of objects you can access each of it's member's properties like you would any Enumerable.

If you want to find the data for the time 1435464000 simply do the following with Enumerable#find

data = forecast["daily"]["data"].find { |d| d["time"] == 1435464000 }
# ensure data exists for time
if data
  data["precipProbability"]
end
Sign up to request clarification or add additional context in comments.

Comments

1

The easiest solution to your query would be

forecast["daily"]["data"].first['precipProbability']

However I suggest you make it inside a loop as there might be no data or more than one data on the array

forecast["daily"]["data"].each do |data|
  puts data['precipProbability'] # or your implementation logic 
end

Comments

1

Anytime you're looking through an iterable like an array, it's nice to use the methods in the Enumerable module

In this case, your data is an array, and you're looking for objects which match a condition (time = 1435464000), so you're looking for the detect method, also known by the more descriptive name find.

data_on_day_you_want = forecast["daily"]["data"].detect{ |h| h["time"] == 1435464000 }
precip = data_on_day_you_want["precipProbability"]

Obviously this can be inlined or broken apart into more methods, but the important thing is the detect. You pass it a block, and it returns the first element of the data array which returns true for that block.

Comments

0
require 'Faraday'
require "JSON"

connection = Faraday.new(JSON.file)
response = connection.get

data_hash = JSON.parse(response.body)["daily"]
data_hash.each do |item|
p item

end

1 Comment

Hello and welcome to SO! While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Please read the tour, and How do I write a good answer?

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.