Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Introduction to JSON Functions | JSON Functions
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Functions in SQL

bookIntroduction to JSON Functions

JSON, or JavaScript Object Notation, is a lightweight data-interchange format that is easy for both humans and machines to read and write. In modern databases, the ability to store and query semi-structured data has become increasingly important. Many applications today capture complex user profiles, preferences, and settings in flexible formats, and the JSON data type allows you to store such information directly within a single column. This approach makes it easier to evolve your database schema over time and to handle data that does not fit neatly into traditional tables.

12345678910111213141516
-- Insert a new user with JSON profile data INSERT INTO user_profiles (user_id, profile_data) VALUES (6, '{ "age": 30, "interests": ["photography", "travel"], "address": { "street": "987 Elm St", "city": "Riverdale", "state": "OH", "zip": "43055" } }'); -- Select all user IDs and their profile data SELECT user_id, profile_data FROM user_profiles;
copy

To work with JSON data in PostgreSQL, you use specialized functions designed for handling JSON and JSONB columns. Three of the most commonly used PostgreSQL functions are:

  • jsonb_extract_path_text: extracts a scalar value (such as a string or number) from a JSONB object at a specified path;
  • jsonb_extract_path: retrieves a JSON object or array from a JSONB column, preserving its structure;
  • jsonb_build_object: constructs a new JSON object from key-value pairs in your query.

These functions let you access specific values inside JSON documents, return nested objects or arrays, or build new JSON objects directly from relational data.

1234
-- Extract the 'city' from the 'address' object within profile_data for user_id 1 using PostgreSQL JSONB functions SELECT jsonb_extract_path_text(profile_data::jsonb, 'address', 'city') AS city FROM user_profiles WHERE user_id = 1;
copy

1. Which PostgreSQL function extracts a scalar value such as a string or number from a JSONB object at a specified path?

2. What does the PostgreSQL function jsonb_extract_path return when used on a JSONB column?

3. Which PostgreSQL function constructs a new JSON object from key-value pairs in a query?

question mark

Which PostgreSQL function extracts a scalar value such as a string or number from a JSONB object at a specified path?

Select the correct answer

question mark

What does the PostgreSQL function jsonb_extract_path return when used on a JSONB column?

Select the correct answer

question mark

Which PostgreSQL function constructs a new JSON object from key-value pairs in a query?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 6. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

How can I extract other fields from the JSON data, like 'state' or 'zip'?

Can I filter users based on a value inside the JSON, such as all users from a specific city?

What is the difference between JSON and JSONB in PostgreSQL?

bookIntroduction to JSON Functions

Swipe to show menu

JSON, or JavaScript Object Notation, is a lightweight data-interchange format that is easy for both humans and machines to read and write. In modern databases, the ability to store and query semi-structured data has become increasingly important. Many applications today capture complex user profiles, preferences, and settings in flexible formats, and the JSON data type allows you to store such information directly within a single column. This approach makes it easier to evolve your database schema over time and to handle data that does not fit neatly into traditional tables.

12345678910111213141516
-- Insert a new user with JSON profile data INSERT INTO user_profiles (user_id, profile_data) VALUES (6, '{ "age": 30, "interests": ["photography", "travel"], "address": { "street": "987 Elm St", "city": "Riverdale", "state": "OH", "zip": "43055" } }'); -- Select all user IDs and their profile data SELECT user_id, profile_data FROM user_profiles;
copy

To work with JSON data in PostgreSQL, you use specialized functions designed for handling JSON and JSONB columns. Three of the most commonly used PostgreSQL functions are:

  • jsonb_extract_path_text: extracts a scalar value (such as a string or number) from a JSONB object at a specified path;
  • jsonb_extract_path: retrieves a JSON object or array from a JSONB column, preserving its structure;
  • jsonb_build_object: constructs a new JSON object from key-value pairs in your query.

These functions let you access specific values inside JSON documents, return nested objects or arrays, or build new JSON objects directly from relational data.

1234
-- Extract the 'city' from the 'address' object within profile_data for user_id 1 using PostgreSQL JSONB functions SELECT jsonb_extract_path_text(profile_data::jsonb, 'address', 'city') AS city FROM user_profiles WHERE user_id = 1;
copy

1. Which PostgreSQL function extracts a scalar value such as a string or number from a JSONB object at a specified path?

2. What does the PostgreSQL function jsonb_extract_path return when used on a JSONB column?

3. Which PostgreSQL function constructs a new JSON object from key-value pairs in a query?

question mark

Which PostgreSQL function extracts a scalar value such as a string or number from a JSONB object at a specified path?

Select the correct answer

question mark

What does the PostgreSQL function jsonb_extract_path return when used on a JSONB column?

Select the correct answer

question mark

Which PostgreSQL function constructs a new JSON object from key-value pairs in a query?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 6. ChapterΒ 1
some-alt