I have a Postgres table which has column of type JSON which has a bunch of JSON objects in them. I want to query the table records and order the results by a value stored in the
You put asc
in the fieldname. There's no key named value asc
in the json, so data ->> 'value asc'
will always return NULL
.
You actually want:
select * from table ORDER BY data->>'value' ASC
to match the json, possibly even:
select *
from table
WHERE data ->> 'name' = 'stuff'
ORDER BY data->>'value' ASC
Use ->
instead of ->>
(->>
gets a JSON object field as text):
select * from my_table ORDER BY data->'some_number' asc;
Try:
ORDER BY cast(data->>'value' as integer) ASC