8

I created a SQL Server Table with 25 columns. One of my columns is actually JSON text, stored as nvarchar(max).

Now I need to able to query this JSON column and parse out the various attributes. I have tried applying JSON_VALUE to my column but am doing something wrong; my query runs but returns NULL for all the values.

The JSON itself looks like:

[
 {
  "lineName":"GHjr",
  "pipeDiameter":"12",
  "pipeLength":"52000",
  "pressure":"15",
  "volume":"107"
 },
 {
  "lineName":"Ks3R",
  "pipeDiameter":"9",
  "pipeLength":"40000",
  "pressure":"15",
  "volume":"80"
 }
]

The SQL I am using is:

select
 DOC_ID, LINE_SPECS,
 JSON_VALUE(LINE_SPECS, '$.lineName')     as line_name,
 JSON_VALUE(LINE_SPECS, '$.pipe_Diameter') as diameter
from dbo.MY_TEST_DOCS
where ISJSON(LINE_SPECS) > 0
  and len(LINE_SPECS) > 3

However, my 2 "parsed" columns are returning all NULL. How do I parse the five attributes from this column?

6
  • Put the JSON_VALUE in strict mode to see if any errors come up. The default mode is lax and won't produce errors, it will just return NULL. learn.microsoft.com/en-us/sql/relational-databases/json/… Also, $.pipe_Diameter I think should be $.pipeDiameter Commented Nov 8, 2017 at 21:10
  • 1
    If you remove the square brackets and make your select match the case-sensitive name in the JSON, it will return a value Commented Nov 8, 2017 at 21:12
  • Actually the brackets might be ok if its an array. Commented Nov 8, 2017 at 21:15
  • 2
    I'm not sure it will work. You can use JSON_QUERY instead, but still need a name before the square brackets to call. Commented Nov 8, 2017 at 21:19
  • Wasn't sure if we were seeing the whole JSON or not. Commented Nov 8, 2017 at 21:20

1 Answer 1

6
Without the [] ISJSON is returning false
With [] ISJSON retuns true
Without the [] JSON_VALUE returns NULLs
With [] JSON_VALUE returns values

dbfddle.uk has sql server 2016 available....

create table test (LINE_SPECS nvarchar(max));

insert into test values (N'
 {
  "lineName":"GHjr",
  "pipeDiameter":"12",
  "pipeLength":"52000",
  "pressure":"15",
  "volume":"107"
 },
 {
  "lineName":"Ks3R",
  "pipeDiameter":"9",
  "pipeLength":"40000",
  "pressure":"15",
  "volume":"80"
 }
');

select * 
from test
where ISJSON(LINE_SPECS) > 0
;

GO
| LINE_SPECS |
| :--------- |
select
      JSON_VALUE(LINE_SPECS, '$.lineName')     as line_name
    , JSON_VALUE(LINE_SPECS, '$.pipeDiameter') as diameter
from test
;
GO
line_name | diameter
:-------- | :-------
GHjr      | 12      

dbfiddle here

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

2 Comments

(and others), your answer(s) really helped!! The JSON I was struggling with included the opening and closing brackets. I created a 2nd column, WITHOUT those brackets, and JSON_VALUE worked as expected. Thank you all for your assistance!
@CarCrazyBen could you please "accept" this answer? Stackoverflows with questions that appear unanswered and this makes it harder for other people to locate answers to their similar problems.

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.