How to parse JSON in postgresql
JsonPostgresqlJson Problem Overview
I have a table in my database, which contains character varying column and this column has json. I need to write a query, which will somehow parse this json into separate columns.
I found json_each function here but I can't understand how to work with it.
Json Solutions
Solution 1 - Json
I figured it out, guys
I can easily write a query
SELECT
id,
data::json->'name' as name
FROM books;
And it will result in
I can also try to get non-existent column
SELECT
id,
data::json->'non_existant' as non_existant
FROM books;
And it this case I will get empty result
Solution 2 - Json
Awesome, thanks for sharing. I found that you can go deeper like:
SELECT
id,
data::json->'name' as name,
data::json->'author' ->> 'last_name' as author
FROM books;