Trying to figure out how to convert multiple rows of data to a single row of KeyValue array
Setup:
using MS SQL SERVER 2017
raw data:
Select s.id, s.FkId, s.name ,s.value from tbl s where FkId = 1024180
id | FkId | name | value
83713 | 1024180 | Name1 | Value1
83714 | 1024180 | Name2 | Value2
Desired result:
{ "Name1":"Value1", "Name2":"Value2" }
Failures/Learnings
I have tried different combos using Json Path and JSON_Array.. neither are resolving to what I need.
Select JSON_ARRAY(s.name,s.value) as result from tbl s where FkId = 1024180
result is two rows with wrappers
row 0 ["Name1","Value1"],
row 1 ["Name2","Value2"]
tried:
Select s.name ,s.value from tbl s where fkid= 1024180 for json path
result:
[{"name":"SpaceSubType","value":"Fee Earner"},{"name":"Department","value":"Property"}]
Is there a reasonable way to get the output I am trying for?
2
Answers
Please try the following solution.
It will work starting from SQL Server 2017 onwards.
SQL
Output
Yet another option with pivot and
FOR JSON PATH
:Output:
Check the demo here.