Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse JSON in postgresql

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.

like image 958
Ivan Ursul Avatar asked Sep 17 '15 08:09

Ivan Ursul


People also ask

Can Postgres read JSON?

What Is PostgreSQL? PostgreSQL is a Relational Database Management System (RDBMS) developed by the PostgreSQL Global Development Group. It has been in use for over 20 years and supports both SQL and JSON for relational and non-relational queries in order to provide flexibility and SQL compliance.

How do I query JSON data?

To query JSON data, you can use standard T-SQL. If you must create a query or report on JSON data, you can easily convert JSON data to rows and columns by calling the OPENJSON rowset function. For more information, see Convert JSON Data to Rows and Columns with OPENJSON (SQL Server).

Should I use Jsonb in Postgres?

JSONB supports indexing the JSON data, and is very efficient at parsing and querying the JSON data. In most cases, when you work with JSON in PostgreSQL, you should be using JSONB.


1 Answers

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

like image 114
Ivan Ursul Avatar answered Sep 24 '22 05:09

Ivan Ursul