Share via

JSON column

Vaishu 41 Reputation points
2022-02-22T18:03:45.837+00:00

Table

EMPID FName LName Title
1 A Z VP
2 B H Director

select Empid,FName,LName,Title from Table
FOR JSON PATH, ROOT('Employee')

My result will be
"Employee" :
[
{"Empid":"1",
"FName":"A",
"LName":"Z",
"Title":"VP"
},

{"Empid":"1",
 "FName":"B",
 "LName":"H",
 "Title":"Director"
}

]

Is it possbile get rows like this

Json Data EMPID FName LName Title

"Employee" : 1 A Z VP
[
{"Empid":"1",
"FName":"A",
"LName":"Z",
"Title":"VP"
}]

"Employee" : 2 B H Director
[
{"Empid":"2",
"FName":"B",
"LName":"H",
"Title":"Director"
}]

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

0 comments No comments

Answer accepted by question author

Viorel 127K Reputation points
2022-02-22T18:10:36.12+00:00

Check this query:

select  
    ( select Empid, FName, LName, Title from Table where EMPID=t.EMPID for json path, root('Employee') ) as [Json Data],
    EMPID, FName, LName, Title
from Table t

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.