Currently playing with JSON fields in postgres 9.3 for the first time and Im running into some difficulty with querying arrays.
The field with the JSON array data type is called 'accounts' and some sample data would be as follows
[{name: "foo", account_id: "123"}, {name: "bar", account_id: "321"}]
I want to be able to find the id of the company that owns account_id 123 for example. The query that I'm having trouble with currently is as follows:
select id from companies where json_array_elements(accounts)->>'account_id' = '123'
This results in an error:
argument of WHERE must not return a set
account_id
of123
then return the corresponding company id" ... ?. – Craig Ringer Feb 10 '14 at 12:19