JSON Functions

SQL · Extra Concepts

Theory

Many systems store semi-structured data such as API responses, event payloads or app settings as JSON text inside a column. JSON functions let SQL read inside that text: JSON_EXTRACT(payload, '$.city') returns the value of the city key, and paths like '$.address.pin' or '$.items[0]' reach nested objects and array elements. If the path does not exist, the result is NULL rather than an error.

Names differ by database. SQLite and MySQL use JSON_EXTRACT and also the -> and ->> operators (->> returns plain text). PostgreSQL uses -> and ->> on json/jsonb columns. SQL Server uses JSON_VALUE for single values and OPENJSON to expand arrays. Snowflake stores JSON in a VARIANT column and reads it with payload:city, using PARSE_JSON to convert text and FLATTEN to expand arrays. Extracted values are often text, so CAST them before doing maths, and move keys you filter on often into real columns for speed.

Why we use it

Event and API data changes shape often, and forcing it into fixed columns is slow to evolve. JSON functions let you keep the flexible document and still query the fields you need with ordinary SQL.

When to use it

Use JSON functions for logs, events, API payloads and optional attributes. If a key is used in most queries, filters or joins, extract it into a typed column or generated column so it can be indexed and validated.

How it works

  1. Row with a JSON text column (payload)
  2. Choose a path: '$.city' or '$.items[0].qty'
  3. Database parses the JSON and follows the path
  4. Does the path exist? Yes: Return the value (CAST if numeric). No: Return NULL.
  5. Use the value in SELECT, WHERE or GROUP BY

Syntax

JSON_EXTRACT(json_column, '$.path')

Example

SELECT JSON_EXTRACT('{"name":"Asha","dept":"IT"}', '$.name');
-- 'Asha'

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic