2

I have the following JSON and i need to query only the name property values.

DECLARE @j NVARCHAR(4000) = N'{  
   "ArrayValue":[
        {
            "name": "XXX",
            "value": 10
        },
        {
            "name": "Memory123",
            "value": 20
        }
    ]
}'

Following is what i get with OPENJSON()

SELECT value as Name
FROM OPENJSON(@j, '$.ArrayValue')

Whcih will get the following output.

Name
---------------------------------------------------
{              "name": "Memory"          }
{              "name": "Memory123"          }

However I need the output in the following format.

Name
-----
Memory
Memory123

Is there any simpler way without using replace?

1 Answer 1

2

You can define ArrayValue as a JSON column, then use CROSS APPLY to get the name values. Here's an example:

DECLARE @j NVARCHAR(4000) = N'{  
   "ArrayValue":[
        {
            "name": "XXX",
            "value": 10
        },
        {
            "name": "Memory123",
            "value": 20
        }
    ]
}'
SELECT b.name
FROM OPENJSON(@j)
WITH (ArrayValue NVARCHAR(MAX) AS JSON) AS a
CROSS APPLY OPENJSON(a.ArrayValue)
WITH ([name] NVARCHAR(100)) AS b
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.