0

Im trying to get the value for "query"

[
    {
        "query"=> "cat",
        "search_type_names"=> [
            "in Handmade"
        ],
        "search_types"=> [
            "handmade"
        ]
    },
    {
        "query"=> "dream catcher",
        "search_type_names"=> [
            "in Handmade"
        ],
        "search_types"=> [
            "handmade"
        ]
    },
    {
        "query"=> "cat ears",
        "search_type_names"=> [
            "in Handmade"
        ],
        "search_types"=> [
            "handmade"
        ]
    },
    {
        "query"=> "cat collar",
        "search_type_names"=> [
            "in Handmade"
        ],
        "search_types"=> [
            "handmade"
        ]
    },
    {
        "query"=> "cat costume",
        "search_type_names"=> [
            "in Handmade"
        ],
        "search_types"=> [
            "handmade"
        ]
    },
    {
        "query"=> "cat shirt",
        "search_type_names"=> [
            "in Handmade"
        ],
        "search_types"=> [
            "handmade"
        ]
    },
    {
        "query"=> "cat ring",
        "search_type_names"=> [
            "in Handmade"
        ],
        "search_types"=> [
            "handmade"
        ]
    },
    {
        "query"=> "cat toys",
        "search_type_names"=> [
            "in Handmade"
        ],
        "search_types"=> [
            "handmade"
        ]
    },
    {
        "query"=> "cat bed",
        "search_type_names"=> [
            "in Handmade"
        ],
        "search_types"=> [
            "handmade"
        ]
    },
    {
        "query"=> "cheshire cat",
        "search_type_names"=> [
            "in Handmade"
        ],
        "search_types"=> [
            "handmade"
        ]
    },
    {
        "query"=> "cat tree",
        "search_type_names"=> [
            "in Handmade"
        ],
        "search_types"=> [
            "handmade"
        ]
    },
    {
        "link"=> "/search/shops?search_query=cat",
        "query"=> "find shop names containing cat",
        "search_type_names"=> [],
        "search_types"=> []
    }
]
4
  • Ive tried json["query"] and it returns a conversion error from integer to string.... Commented Oct 2, 2015 at 1:48
  • Your input is an array. If you wanted it to be a string, you need quotes. It's helpful to assign all inputs to a variable (e.g., arr = [...] or str = "..."). That allows those offering answers or posting comments to refer to the variable without having to define it. Also, when you report an exception, be specific about the exception message and the line where it was raised. Commented Oct 2, 2015 at 2:13
  • Reduce your input data to the bare minimum useful to demonstrate the problem you are seeing. Anything beyond that only wastes our time helping you. Also, as is, it looks like you want us to write the code for you. What did you write? Commented Oct 2, 2015 at 2:40
  • This is the minimum useful information to get the answer I need. So if I add all the code before this problem it would waste your time. And writing this wasn't a waste of your time?... Commented Oct 8, 2015 at 15:48

4 Answers 4

2

It appears you've already converted the JSON string to an array, which I call arr, in which case:

arr.map { |h| h["query"] }
  #=> ["cat", "dream catcher", "cat ears", "cat collar", "cat costume",
  #    "cat shirt", "cat ring", "cat toys", "cat bed", "cheshire cat",
  #    "cat tree", "find shop names containing cat"] 
Sign up to request clarification or add additional context in comments.

Comments

1

Since that JSON is an array, you'll have to first choose one of the elements from the array, and then use the key for the hash to extract 'query'

json.first['query']
json[5]['query']

Comments

0

Letting json_array is the array of JSON that you have, you can get the querys this way too:

json_array.each { |a| puts a['query'] }

Comments

0

You can try this.Lets you have a a that the array of JSON

irb(main):151:0> a.collect{|h| h["query"]}
=> ["cat", "dream catcher", "cat ears", "cat collar", "cat costume", "cat shirt", "cat ring", "cat toys", "cat bed", "cheshire cat", "cat tree", "find shop names containing cat"]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.