Skip to main content
The 2024 Developer Survey results are live! See the results
added 9 characters in body
Source Link
Zhorov
  • 29.6k
  • 6
  • 30
  • 54

You need to execute the following statement in a different way. Note, that when you use OPENJSON()OPENJSON() with explicit schema you need to use AS JSONAS JSON to specify that the referenced property contains an inner JSON object or array. NoteOf course, that you may use JSON_QUERY() to get the same result:

JSON:

DECLARE @json nvarchar(max) =
'{
    "firstName": "John",
    "lastName" : "doe",
    "address"  : {
        "streetAddress": "naist street",
        "city"         : "Nara",
        "postalCode"   : "630-0192"
    }
}'

Statement (using OPENJSON()):

SELECT *
FROM OPENJSON(@json) 
WITH ([Type] NVARCHAR(max) '$.address' AS JSON);

Statement (using JSON_QUERY())

SELECT JSON_QUERY(@json, '$.address') AS [Type]

Result:

Type
{
        "streetAddress": "naist street",
        "city"         : "Nara",
        "postalCode"   : "630-0192"
    }

You need to execute the following statement. Note, that when you use OPENJSON() with explicit schema you need to use AS JSON to specify that the referenced property contains an inner JSON object or array. Note, that you may use JSON_QUERY() to get the same result:

JSON:

DECLARE @json nvarchar(max) =
'{
    "firstName": "John",
    "lastName" : "doe",
    "address"  : {
        "streetAddress": "naist street",
        "city"         : "Nara",
        "postalCode"   : "630-0192"
    }
}'

Statement (using OPENJSON()):

SELECT *
FROM OPENJSON(@json) 
WITH ([Type] NVARCHAR(max) '$.address' AS JSON);

Statement (using JSON_QUERY())

SELECT JSON_QUERY(@json, '$.address') AS [Type]

Result:

Type
{
        "streetAddress": "naist street",
        "city"         : "Nara",
        "postalCode"   : "630-0192"
    }

You need to execute the statement in a different way. Note, that when you use OPENJSON() with explicit schema you need to use AS JSON to specify that the referenced property contains an inner JSON object or array. Of course, you may use JSON_QUERY() to get the same result:

JSON:

DECLARE @json nvarchar(max) =
'{
    "firstName": "John",
    "lastName" : "doe",
    "address"  : {
        "streetAddress": "naist street",
        "city"         : "Nara",
        "postalCode"   : "630-0192"
    }
}'

Statement (using OPENJSON()):

SELECT *
FROM OPENJSON(@json) 
WITH ([Type] NVARCHAR(max) '$.address' AS JSON);

Statement (using JSON_QUERY())

SELECT JSON_QUERY(@json, '$.address') AS [Type]

Result:

Type
{
        "streetAddress": "naist street",
        "city"         : "Nara",
        "postalCode"   : "630-0192"
    }
Source Link
Zhorov
  • 29.6k
  • 6
  • 30
  • 54

You need to execute the following statement. Note, that when you use OPENJSON() with explicit schema you need to use AS JSON to specify that the referenced property contains an inner JSON object or array. Note, that you may use JSON_QUERY() to get the same result:

JSON:

DECLARE @json nvarchar(max) =
'{
    "firstName": "John",
    "lastName" : "doe",
    "address"  : {
        "streetAddress": "naist street",
        "city"         : "Nara",
        "postalCode"   : "630-0192"
    }
}'

Statement (using OPENJSON()):

SELECT *
FROM OPENJSON(@json) 
WITH ([Type] NVARCHAR(max) '$.address' AS JSON);

Statement (using JSON_QUERY())

SELECT JSON_QUERY(@json, '$.address') AS [Type]

Result:

Type
{
        "streetAddress": "naist street",
        "city"         : "Nara",
        "postalCode"   : "630-0192"
    }