0

I have a table which have many column. One column has json value like

{
  "RequiredForCompany":1, 
  "RequiredOnScreen":0,
  "Editable":[],
  "Visible":[], 
  "Expression":{},
  "GroupFields":[142,156]
}

I want to query into this json value with selecting others column. My query like as

SELECT 
    [Name],
    JSON_VALUE(FieldAttributes, '$.GroupFields') AS GroupFields
FROM 
    [std].[Field]

But it returns null for groupfields. Any way to query into this value?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Md. Abdul Alim
  • 707
  • 1
  • 6
  • 19
  • 1
    Here is good reference: http://www.allaboutmssql.com/2015/12/sql-server-json-examples-jsonquery.html – slon Sep 24 '18 at 04:37

2 Answers2

1

your GroupFields (array)is sequence values container that's why you have to select like below

DECLARE @jsonInfo1 NVARCHAR(MAX) =N'{
"RequiredForCompany":1,
"RequiredOnScreen":0,
"Editable":[], 
"Visible":[],
"Expression":{},
"GroupFields":[142,156]

}';


select JSON_VALUE(@jsonInfo1, '$.GroupFields[0]') as g;

https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=7eba8bf042a27253440ce4b41f440979

So in your case

SELECT [Name],
  JSON_VALUE(FieldAttributes,'$.GroupFields[0]') AS GroupFields
FROM [std].[Field]
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63
0

Finally I got a solution by the help of @Zaynul Abadin Tuhin. My query is

SELECT [Name], STUFF((SELECT ','+[Value] FROM 
  OPENJSON (FieldAttributes,'$.GroupFields') FOR XML PATH ('')),1,1,'') AS GroupFields
FROM [std].[Field]

It's working fine.

Md. Abdul Alim
  • 707
  • 1
  • 6
  • 19