EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL JSON
 

PostgreSQL JSON

Updated May 9, 2023

PostgreSQL JSON

 

 

Definition of PostgreSQL JSON

JSON is an abbreviation of JavaScript Object Notation. JSON stores value in key-value pair; it is an open standard format. We generally prefer JSON for sending/receiving or exchanging data between servers and in web applications. The data within JSON is in text format, which is easily human-readable. PostgreSQL version 9.2 introduced support for the native JSON data type. PostgreSQL provides various methods and operators to work with JSON data.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

column_name json 

Explanation: The name of the column whose data type will be JSON.

How JSON Works in PostgreSQL?

  • We need to make sure the given data is in a valid JSON format before adding it to the table.
  • If JSON data is incorrect, then it will throw an error.
  • PostgreSQL provides the two native operators to work with JSON data.
  • The -> operator Returns JSON data in the form of a key.
  • The ->> operator: Returns JSON data in the form of text.

How to Insert JSON Data?

To understand the insertion of JSON data, let us create a ‘student’ table with the following structure.

The student table consists of two columns:

  • stud_id: The column is the primary key column that uniquely identifies the student.
  • stud_data: The column which stores the student’s information in the form of JSON.

Let’s create the table by using the CREATE TABLE statement:

CREATE TABLE student (
stud_id serial NOT NULL PRIMARY KEY,
stud_data json NOT NULL
);

Now we will insert the data into the stud_data column, which is of type JSON. Before adding JSON data to the table, we need to ensure the given data is invalid in JSON format. Now insert the JSON data with the help of the following INSERT statement, which will add a new row into the ‘student’ table.

INSERT INTO student (stud_data)
VALUES
(
'{
"name": "Oliver Jake",
"information":
{
"mobile_number": "9999999999",
"branch": "Computer",
"rank":12
}
}'
);

After executing the above statement, illustrate the student table’s content using the following snapshot and SQL statement.

select * from student;

Output:

PostgreSQL JSON-1.1

We can insert multiple rows in the table using the following INSERT statement:

INSERT INTO student (stud_data)
VALUES
(
'{
"name": "Jack Connor",
"information":
{
"mobile_number": "9999999910",
"branch": "Computer",
"rank":1
}
}'
),
(
'{
"name": "Harry Callum",
"information":
{
"mobile_number": "9999999911",
"branch": "Civil",
"rank":2
}
}'
),
(
'{
"name": "Jacob John",
"information":
{
"mobile_number": "9999999912",
"branch": "Electrical",
"rank":6
}
}'
);
select * from student;

We can fetch the data from the student table by using the following snapshot and SQL statements.

Output:

PostgreSQL JSON-1.2

Examples of PostgreSQL JSON

We have created a student table in the above section; let’s use the same for understanding the following examples.

Example #1 – Get all students in the form of JSON key

SELECT
stud_data -> 'name' AS StudentName
FROM
student;

Output:

PostgreSQL JSON-2.1

Example #2 – Get all students in the form of JSON text

SELECT
stud_data ->> 'name' AS StudentName
FROM
student;

Output:

PostgreSQL JSON-2.2

Example #3 – Get specific JSON node using operators

SELECT
stud_data -> 'information' ->> 'rank' as rank
FROM
student
ORDER BY
rank;

Output:

PostgreSQL JSON-2.3

Example #4 – Use JSON operator in WHERE clause

In order to filter rows from the result set, we can use the JSON operators in the WHERE clause. Consider the following example, which gives us the record whose branch is Computer by using the following statement.

SELECT
stud_data ->> 'name' AS StudentName
FROM
student
WHERE
stud_data -> 'information' ->> 'branch' = 'Computer'

Output:

PostgreSQL JSON-2.4

Example #5 – PostgreSQL JSON functions

PostgreSQL provides us with some functions to handle JSON data.

json_each function

By using the json_each() function, we can expand the outermost JSON object into a set of key-value pairs as follows:

SELECT
json_each (stud_data)
FROM
student;

We can use the json_each_text() function to get a set of key-value pairs as text.

json_object_keys function

We can use the json_object_keys() function to get a set of keys in the outermost JSON object as follows:

SELECT
json_object_keys (stud_data->'information')
FROM
student;

Output:

Output-2.5

json_typeof function

With the help of the function json_typeof(), we can get the type of the outermost JSON value as a string. The type of JSON value can be a boolean, number null, string, object, and array.

We can get the data type of the information using the following statement:

SELECT
json_typeof (stud_data->'information')
FROM
student;

Output:

Output-2.6

We can get the data type rank field of the nested information JSON object using the following statement:

SELECT
json_typeof (stud_data->'information'->'rank')
FROM
student;

Output:

Output-2.7

Advantages of using JSON in PostgreSQL

Advantages of using JSON in PostgreSQL are given below:

  • Avoid complicated joins.
  • Parsing of JSON data is quite easier and faster execution.
  • Compatible with various database management systems.
  • Javascript Notation Objects are faster and very easy to read and understand.
  • The data within the JSON object is separated by a comma, making it easily understandable.
  • JSON is lightweight for data exchange.

Conclusion

From the above article, we hope you understand how to use the PostgreSQL JSON data type and how the PostgreSQL JSON data type works to store the data in key-value pair. Also, we have added some examples of PostgreSQL JSON to understand it in detail.

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL JSON” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. PostgreSQL Features
  2. Versions PostgreSQL
  3. PostgreSQL Data Types
  4. Examples of PostgreSQL Notify
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW