Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract value from a JSON string with no Key?

I have a JSON column in one of the tables, and the JSON column has no key or property, only the value.

I tried to parse the column with JSON_Query and JSON_Value, but both of these functions only work if the JSON string has a key, but in my situation, the JSON string has no key.

So how can I parse the column from the top table to the bottom table in SQL Server like the image below?

enter image description here

like image 784
Duyan Zhen Avatar asked Sep 07 '25 17:09

Duyan Zhen


1 Answers

Please try this:

DECLARE @Table TABLE (ID INT, [JSONColumn] NVARCHAR(MAX));
INSERT INTO @Table(ID,[JSONColumn])VALUES
     (151616,'["B0107C57WO","B066EYU4IY"]')
    ,(151617,'["B0088MD64S"]')
;

SELECT t.ID,j.[value]
FROM @Table t
CROSS APPLY OPENJSON(t.JSONColumn) j
;
like image 144
Vitaly Borisov Avatar answered Sep 10 '25 02:09

Vitaly Borisov