How to parse JSON in postgresql

JsonPostgresql

Json 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

if I have a table books enter image description here

I can easily write a query

SELECT 
   id, 
   data::json->'name' as name
FROM books;

And it will result in

enter image description here

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

enter image description here

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;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIvan UrsulView Question on Stackoverflow
Solution 1 - JsonIvan UrsulView Answer on Stackoverflow
Solution 2 - Jsonhit3kView Answer on Stackoverflow