
- SQL - Home
- SQL - Roadmap
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL - Comments
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Cheatsheet
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL - JSON_OBJECT() Function
The SQL JSON_OBJECT() function is used to create JSON objects from the results of evaluating the SQL expressions of the arguments. It constructs the text of a JSON object from none or more expressions.
A JSON object contains a key/value pair. Each key is represented as a string in JSON, and the value can be of any type. The Keys and values are separated by a colon and each key/value pair is separated by a comma.
A curly brace { represents a JSON object.
Syntax
Following is the syntax of SQL JSON_OBJECT() function −
JSON_OBJECT ( [ <json_key_value> [,...n] ] [ json_null_clause ] ) <json_key_value> ::= json_key_name : value_expression <json_null_clause> ::= NULL ON NULL | ABSENT ON NULL
Parameters
They are three types of parameters −
json_key_name − It is a character expression that defines a JSON key name value.
-
value_expression − It is an expression that defines the value of the JSON key.
json_null_clause − It can be used to control the behavior of the JSON_OBJECT function when value_expression is NULL. The NULL ON NULL option when generating a JSON key value converts a SQL NULL value to a JSON null value. The ABSENT ON NULL option omits the entire key if the value is NULL.
Return Value
It returns a valid JSON object string of type nvarchar(max).
Example
Following is an example returns the JSON object with two keys −
SELECT JSON_OBJECT('name':'value', 'type':4) AS JSON_OBJECT;
Output
This will display the following result −
+-----------------------------+ | JSON_OBJECT | +-----------------------------+ | {"name":"value","type":4} | +-----------------------------+
Example
The following example returns a JSON object with one key because the value of one of the keys is NULL and the ABSENT ON NULL option is specified −
SELECT JSON_OBJECT('name':'value', 'type':NULL ABSENT ON NULL) AS JSON_OBJECT;
Output
This will display the following result −
+-------------------+ | JSON_OBJECT | +-------------------+ | {"name":"value"} | +-------------------+
Example
Following is an example returns an empty JSON object −
SELECT JSON_OBJECT() AS JSON_OBJECT;
Output
This will display the following result −
+-------------------+ | JSON_OBJECT | +-------------------+ | {} | +-------------------+
Example
Following is an example returns a JSON object with two keys. One key contains a JSON string and the other key contains a JSON array −
SELECT JSON_OBJECT('name':'value', 'type':JSON_ARRAY(4, 6)) AS JSON_OBJECT;
Output
This will display the following result −
+-----------------------------+ | JSON_OBJECT | +-----------------------------+ |{"name":"value","type":[4,6]}| +-----------------------------+
Example
Following is an example returns a JSON object with two keys. One key contains a JSON string and the other key contains a JSON object−
SELECT JSON_OBJECT('name':'value', 'type':JSON_OBJECT('type_id':4, 'name':'t')) AS JSON_OBJECT;
Output
This will display the following result −
+------------------------------------------------+ | JSON_OBJECT | +------------------------------------------------+ |{"name":"value","type":{"type_id":4,"name":"t"}}| +------------------------------------------------+