Reputation: 58
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"=> []
}
]
Upvotes: 0
Views: 66
Reputation: 12320
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"]
Upvotes: 0
Reputation: 34338
Letting json_array
is the array of JSON that you have, you can get the query
s this way too:
json_array.each { |a| puts a['query'] }
Upvotes: 0
Reputation: 1854
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']
Upvotes: 1
Reputation: 110675
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"]
Upvotes: 2