Hi,
So I'm having a little play with JSON as I need to store some data as JSON in a new db we have.
I created a little sample, where I use tempdb, create a table, but some JSON in there and read it back out.
I've attached the script to this, my issues are;
I've no doubt the issue is the formatting of the JSON but I cant for the life of me see what I've missed.
Can anyone help me out and tell me what I'm missing.
Thanks,
Nic
So, to fix your JSON, you need [ ] around the whole thing as it is an array.
Something like this:
SET @SampleJSON = '[
{
"name": "John",
"surname": "Doe",
"skills": ["SQL", "C#", "Azure"]
},
{
"name": "Jane",
"surname": "Doe",
"skills": ["SQL", "C#", "Azure"]
}
]'
If you do that, then your ISJSON will return 1 like what you want as it is now valid JSON format. This will break the SELECT for the value, but we can fix that as it wasn't working right anywas. JSON_VALUE returns a single value, not a set. So if you want the first value, you would use $[0].name. For example:
USE [Admin]
DECLARE @Response TABLE
(
ResponseJSON NVARCHAR(MAX)
)
DECLARE @SampleJSON NVARCHAR(MAX)
SET @SampleJSON = '[
{
"name": "John",
"surname": "Doe",
"skills": ["SQL", "C#", "Azure"]
},
{
"name": "Jane",
"surname": "Doe",
"skills": ["SQL", "C#", "Azure"]
}
]'
--Put the sample data in
INSERT INTO @Response
SELECT @SampleJSON
--Check if the JSON is valid
SELECT ResponseJSON,ISJSON(ResponseJSON)
FROM @Response
SELECT JSON_VALUE(ResponseJSON,'$[0].name') AS Name
FROM @Response
--WHERE ISJSON(ResponseJSON) > 0
If you want ALL of the "names" from that JSON object, you will need to do it with a CROSS APPLY like so:
SELECT [JSONData].[name]
FROM @response
CROSS APPLY
OPENJSON(ResponseJSON) WITH (name VARCHAR(255) '$.name') JSONData;
Does that help?
The above is all just my opinion on what you should do.
As with all advice you find on a random internet forum - you shouldn't blindly follow it. Always test on a test server to see if there is negative side effects before making changes to live!
I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.
July 8, 2020 at 7:42 am
Thank you very much.
This is perfect, it not only gives me the desired result but also explains the process behind it, saving me asking more questions later on.
Thank you for taking the time to formulate such a well written response.
Nic
July 8, 2020 at 12:03 pm
This was removed by the editor as SPAM
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply