How can I read value from json file in that field name contains space using OPENJSON in Sql Server 2016. See the below code:
DECLARE @json NVARCHAR(MAX)
SET @json = N'{ "full name" : "Jayesh Tank"}';
SELECT * FROM OPENJSON(@json) WITH ( [name] [varchar](60) '$.full name')
Also another sample code in that space is after field name.
SET @json = N'{ "name " : "abc"}';
SELECT * FROM OPENJSON(@json) WITH ( [name] [varchar](60) '$.name')
'$.name'
will return null.Is there way to read this value?
Generally it is a bad idea to use spaces in the attribute name.
I would leave out the [
]
from your OPENJSON name and varchar(60) - source MSDN OPENJSON.
Now to actually answer your question:
You need to format your attribute with double quotes in the WITH
clause:
@DECLARE @json NVARCHAR(MAX);
SET @json=N'{ "full name" : "Jayesh Tank"}';
SELECT * FROM OPENJSON(@json) WITH (name varchar(60) '$."full name"')
for the second one:
SET @json = N'{ "name " : "abc"}';
SELECT * FROM OPENJSON(@json) WITH ( name varchar(60)'$."name "')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With