0

I have a JSON array that looks like this:

response = {
    "items"=>[
        {
            "tags"=>[
                "random"
            ],
            "timestamp"=>12345,
            "storage"=>{
                "url"=>"https://example.com/example",
                "key"=>"mykeys"
            },
            "envelope"=>{

            },
            "log-level"=>"info",
            "id"=>"random_id_test_1",
            "campaigns"=>[

            ],
            "user-variables"=>{

            },
            "flags"=>{
                "is-test-mode"=>false
            },
            "message"=>{
                "headers"=>{
                    "to"=>"[email protected]",
                    "message-id"=>"[email protected]",
                    "from"=>"[email protected]",
                    "subject"=>"new subject"
                },
                "attachments"=>[

                ],
                "recipients"=>[
                    "[email protected]"
                ],
                "size"=>4444
            },
            "event"=>"stored"
        },
        {
            "tags"=>[
                "flowerPower"
            ],
            "timestamp"=>567890,
            "storage"=>{
                "url"=>"https://yahoo.com",
                "key"=>"some_really_cool_keys_go_here"
            },
            "envelope"=>{

            },
            "log-level"=>"info",
            "id"=>"some_really_cool_ids_go_here",
            "campaigns"=>[

            ],
            "user-variables"=>{

            },
            "flags"=>{
                "is-test-mode"=>false
            },
            "message"=>{
                "headers"=>{
                    "to"=>"[email protected]",
                    "message-id"=>"[email protected]",
                    "from"=>"[email protected]",
                    "subject"=>"email_looks_good"
                },
                "attachments"=>[

                ],
                "recipients"=>[
                    "[email protected]"
                ],
                "size"=>2222
            },
            "event"=>"stored"

        }]
}

I am trying to obtain the "storage" "url" based on the "to" email. How do I iterate through this array where x is just the element in the array

response['items'][x]["message"]["headers"]["to"]

Once I find the specific email that I need, it will stop and return the value of x which is the element number. I was going to use that value for x and call response['items'][x]['storage']['url'] which will return the string for the URL.

I thought about doing this but there's gotta be a better way:

x = 0
user_email = [email protected]
while user_email != response['items'][x]["message"]["headers"]["to"] do
  x+=1
  value = x
  puts value
end
1
  • 1
    Welcome to Stack Overflow. It's important to strip data to the minimum necessary to demonstrate the problem. Anything beyond that only slows our ability to help you. Commented Aug 27, 2015 at 22:50

3 Answers 3

2
target = 
  response['items'].detect do |i| 
    i['message']['headers']['to'] == '[email protected]'
  end

then

target['storage']['url']
Sign up to request clarification or add additional context in comments.

6 Comments

thanks, I was able to verify that in irb. I was ripping my hair out trying everything in irb wasn't working. I learned a new method today. "detect" Thanks so much!
Are you sure the OP didn't want i['message']['headers'].key?('to')? Do we know that target != nil?
Are you sure that's the same question?
@CarySwoveland - yeah, doing that will make the code more robust. Ideally, it should never be nil. But you never know. if it is nil it won't be too catastrophic. Thanks for the comment.
@satoru - could i use a regex? (trying to do domain matching: i['message']['headers']['to'] == /example.com/
|
1

This is another option by creating Hash with key of to's email. And on basis of it fetch required information like this:

email_hash = Hash.new
response["items"].each do |i|
   email_hash[i["message"]["headers"]["to"]] = i
end

Now if you want to fetch "storage" "url" then simply do:

user_email = "[email protected]"
puts email_hash[user_email]["storage"]["url"] if email_hash[user_email]

#=> "https://yahoo.com"

Comments

0

You can use it as @Satoru suggested. As a suggestion, if you use case involves complex queries on json data (more complex than this), then you can store your data in mongodb, and can elegantly query anything.

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.