0

I have a string column in an SQL-Server table on Azure which contains the following data:

 {
        "status": "success",
        "data": [
            {
                "name": "Jane",
                "type": 0
            },
            {
                "name": "John",
                "type": 0
            }
        ]
    }

How can it be transformed into a comma-separated string containing "Jane, John"?

2
  • Is "data" always going to be the same size? For example will it always be two items or do you need it to be dynamic? Commented May 20, 2021 at 15:05
  • Hi, "data" has no constant size, it needs to be dynamic. Commented May 20, 2021 at 15:36

1 Answer 1

1

Here how to achieve this via a snippet of some of my older code, you should be able to loop through the table and do this for each row. There may be a quicker way of doing it but this will work.

DECLARE @JSON NVARCHAR(200) = '{"status": "success", "data": [{"name": "Jane", "type": 0},{"name": "John", "type": 0}]}',
    @result nvarchar(max) = ''


SELECT @result = @result + [value] + N', '
FROM (
    SELECT DISTINCT data.value
    FROM OPENJSON(@JSON, '$.data') as jsondata
    CROSS APPLY OPENJSON(jsondata.value) as data
    WHERE data.[key] = 'name') a

select @result = substring(@result, 1, (LEN(@result)-1))

select @result
Sign up to request clarification or add additional context in comments.

Comments

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.