2

My json for an attribute in a record looks as so:

 { foo: bar,   
   foo1: [{key1: value, key2: value},
         {key1: value, key2: value}]
  }

i can query for foo just fine by using

Model.where("attribute->>'foo' = ?", "bar")

Having some trouble querying for values in in key1 and key2

1 Answer 1

2

Take a look here postrgeSQL

You can use this operator #>> to perform what you need, as the link says 'Get JSON object at specified path as text', for example:

'{"a":[1,2,3],"b":[4,5,6]}'::json#>>'{a,2}'

Model.where("attribute#>>'{foo1, key1}' = ?", "bar")

You should just adapt to your needs now. Hope it helped!

Sign up to request clarification or add additional context in comments.

2 Comments

I am still getting back an empty array when querying with this criteria. Does it matter if i have hashes within the array? e.g. your example is "a": [1,2,3], mine would be "a":[{1:2}, {3:4}]
Have you tried to use this operator #>, instead ? At the link that I posted it shows an example to retrieve the hash value.

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.