1

It is probably something stupid what I did not see, but I can't manage to get JSON_VALUE or JSON_QUERY to work as I expect how it should work:

DECLARE @json nvarchar(max) = N'{"1031":"test-de","1033":"test-en","1036":"test-fr"}'

SELECT *
FROM OPENJSON(@json)

returns correct 3 rows.

DECLARE @json nvarchar(max) = N'{"1031":"test-de","1033":"test-en","1036":"test-fr"}'
SELECT ISJSON(@json) is_json
  , JSON_VALUE(@json, '$') label
  , JSON_QUERY(@json, '$') label2

returns 1 row, but label is NULL and label2 shows the whole string. OK, but:

DECLARE @json nvarchar(max) = N'{"1031":"test-de","1033":"test-en","1036":"test-fr"}'
SELECT ISJSON(@json) is_json
  , JSON_VALUE(@json, '$.1031') label
  , JSON_QUERY(@json, '$.1033') label2

returns 0 rows.

EDIT

I did expect to get results like this:

is_json, label, label2
1, test-de, test-en
2
  • What resultset are you after then? Commented Jun 7, 2019 at 9:48
  • @Larnu updated question to clarify Commented Jun 7, 2019 at 9:50

1 Answer 1

1

As your Key starts with a number, you need to quote it. I think what you're after is:

DECLARE @json nvarchar(max) = N'{"1031":"test-de","1033":"test-en","1036":"test-fr"}'

SELECT ISJSON(@json) AS is_json,
       JSON_VALUE(@json, '$."1031"') AS label,
       JSON_VALUE(@json, '$."1033"') AS label2;

Alternatively you could use a WITH:

DECLARE @json nvarchar(max) = N'{"1031":"test-de","1033":"test-en","1036":"test-fr"}'

SELECT ISJSON(@json) is_json,
       J.label1,
       J.label2
FROM OPENJSON(@json)
     WITH (label1 varchar(7) '$."1031"',
           label2 varchar(7) '$."1033"') J;
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.