JSON Path Expressions - SQL Server (original) (raw)

Applies to: SQL Server 2016 (13.x) and later Azure SQL Database Azure SQL Managed Instance Azure Synapse Analytics (serverless SQL pool only)

Use JSON path expressions to reference the properties of JSON objects.

You have to provide a path expression when you call the following functions.

Parts of a path expression

A path expression has two components.

  1. The optional path mode, with a value of lax or strict.
  2. The path itself.

Path mode

At the beginning of the path expression, optionally declare the path mode by specifying the keyword lax or strict. The default is lax.

The following query explicitly specifies lax mode in the path expression.

DECLARE @json NVARCHAR(MAX);
SET @json=N'{ ... }';

SELECT * FROM OPENJSON(@json, N'lax $.info');

Path

After the optional path mode declaration, specify the path itself.

Examples

The examples in this section reference the following JSON text.

{
    "people": [{
        "name": "John",
        "surname": "Doe"
    }, {
        "name": "Jane",
        "surname": null,
        "active": true
    }]
}

The following table shows some examples of path expressions.

Path expression Value
$.people[0].name John
$.people[1] { "name": "Jane", "surname": null, "active": true }
$.people[1].surname null
$ { "people": [ { "name": "John", "surname": "Doe" }, { "name": "Jane", "surname": null, "active": true } ] }

How built-in functions handle duplicate paths

If the JSON text contains duplicate properties - for example, two keys with the same name on the same level - the JSON_VALUE and JSON_QUERY functions return only the first value that matches the path. To parse a JSON object that contains duplicate keys and return all values, use OPENJSON, as shown in the following example.

DECLARE @json NVARCHAR(MAX);
SET @json=N'{"person":{"info":{"name":"John", "name":"Jack"}}}';

SELECT value
  FROM OPENJSON(@json,'$.person.info');

Learn more about JSON in SQL Server and Azure SQL Database

Microsoft videos

Note

Some of the video links in this section may not work at this time. Microsoft is migrating content formerly on Channel 9 to a new platform. We will update the links as the videos are migrated to the new platform.

For a visual introduction to the built-in JSON support in SQL Server and Azure SQL Database, see the following videos:

See Also

OPENJSON (Transact-SQL)
JSON_VALUE (Transact-SQL)
JSON_QUERY (Transact-SQL)