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?

1

2 Answers 2

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]
Sign up to request clarification or add additional context in comments.

5 Comments

@Md.AbdulAlim dbfiddle.uk/… could you please check this link
I want get all data into GroupFields not only first value. How can?
@Md.AbdulAlim dbfiddle.uk/… check the 2nd query in this link
Thanks for your help. I got a solution by using your process
Your answer is helpful but not complete. I voted your answer.
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.

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.