Skip to main content
The 2024 Developer Survey results are live! See the results
deleted 23 characters in body; edited title
Source Link
Dale K
  • 26.7k
  • 15
  • 44
  • 73

Select Json Object as nvarchar(max) in SQL Server JSON_VALUE

I do not know how to extract a path of jsonJSON as nvarchar(max). It always returns null.

I do not want to specify the fields to get because I have no idea the field names. So the idea here is select it as a string and process them later.

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

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

The expected result is that the Type column contains the whole jsonJSON object of address as string

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

Thanks for any help

Select Json Object as nvarchar(max) in SQL Server JSON_VALUE

I do not know how to extract a path of json as nvarchar(max). It always returns null.

I do not want to specify the fields to get because I have no idea the field names. So the idea here is select it as a string and process them later.

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

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

The expected result is that the Type column contains the whole json object of address as string

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

Thanks for any help

Select Json Object as nvarchar(max) in JSON_VALUE

I do not know how to extract a path of JSON as nvarchar(max). It always returns null.

I do not want to specify the fields to get because I have no idea the field names. So the idea here is select it as a string and process them later.

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

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

The expected result is that the Type column contains the whole JSON object of address as string

{ "streetAddress": "naist street", "city": "Nara", "postalCode" : "630-0192" }
added 6 characters in body
Source Link
marc_s
  • 748k
  • 180
  • 1.4k
  • 1.5k

I do not know how to extract a path of json as navarchar(max)nvarchar(max). It always returnreturns null.

I do not want to specify the fields to get because I have no idea the field names. So the idea here is select it as a string and process them later.

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

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

The expected result is that the Type column contains the whole json object of address as string

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

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

Thanks for helpsany help

I do not know how to extract a path of json as navarchar(max). It always return null.

I do not want to specify the fields to get because I have no idea the field names. So the idea here is select it as a string and process them later.

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

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

The expected result is that the Type column contains the whole json object of address as string

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

Thanks for helps

I do not know how to extract a path of json as nvarchar(max). It always returns null.

I do not want to specify the fields to get because I have no idea the field names. So the idea here is select it as a string and process them later.

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

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

The expected result is that the Type column contains the whole json object of address as string

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

Thanks for any help

Source Link
Hieu Le
  • 1.1k
  • 10
  • 21

Select Json Object as nvarchar(max) in SQL Server JSON_VALUE

I do not know how to extract a path of json as navarchar(max). It always return null.

I do not want to specify the fields to get because I have no idea the field names. So the idea here is select it as a string and process them later.

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

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

The expected result is that the Type column contains the whole json object of address as string

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

Thanks for helps