0

This is my query:

SELECT
    JSON_QUERY(MyStringColumnWithJson, '$.Images') AS images
FROM MyTable

which returns a single field with the JSON data shown here below:

"{ 
   "Images":
   [
     {"Name":"test1.jpeg","Description":"originalName1.jpeg"}, 
     {"Name":"test2.jpeg","Description":"originalName2.jpeg"}, 
     {"Name":"test3.jpeg","Description":"originalName3.jpeg"}
   ]
}"

How can I read the images result row by row into a temporary table structure?

0

1 Answer 1

3

Use OPENJSON which returns a data set, not JSON_VALUE, which returns a scalar value. For example:

DECLARE @JSON nvarchar(MAX) = N'{ 
"Images":
   [
     {"Name":"test1.jpeg","Description":"originalName1.jpeg"}, 
     {"Name":"test2.jpeg","Description":"originalName2.jpeg"}, 
     {"Name":"test3.jpeg","Description":"originalName3.jpeg"}
   ]
}';

SELECT *
FROM OPENJSON(@JSON, '$.Images')
     WITH (Name nvarchar(128),
           Description nvarchar(128))OJ;

SELECT I.[Name],
       I.Description
FROM MyTable MT
     CROSS APPLY OPENJSON(MT.YourJsonColumn, '$.Images')
                 WITH (Name nvarchar(128),
                       Description nvarchar(128)) I;
Sign up to request clarification or add additional context in comments.

8 Comments

this is like all samples I found... they base on a variable containing json... BUT my json is in an sql field! How would you do that?
Just replace @JSON with your column name, @HelloWorld (obviously with the table already referenced in the FROM).
what is OJ at the end?
An Alias, @HelloWorld ... O for Open, J for JSON. You can use something else if you prefer. For example I for Images.
sorry i dont get it...its really confusing. Do i have now 2 select statements? Can you pls use my original code and modify it , thx!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.