If you have a simple table like:
Id Name Age
1 Saeed 32
2 John 28
3 David 34
Then you can create a JSON using For XML
in SQL Server just like:
select '{ name : "' + Name + '", age : ' + age + ' }'
from People
where Id = 1
for xml path('')
This is easy, because columns are known beforehand. However, I'm stuck at creating JSON from an EAV table. For example, if the table is:
Id EntityId Key Value
1 1 Name Saeed
2 1 Age 32
3 1 Gender Male
4 1 Key1 Value1
5 1 Key2 Value2
How can I create this JSON?
{ Name: "Saeed", Age: 32, Gender: "Male", Key1: "Value1", Key2: "Value2" }
From this query:
select *
from PeopleEav
where EntityId = 1
Please note that the number of keys is variable (it's an EAV table).
Try this one -
DECLARE @PeopleEav TABLE
(
Id INT IDENTITY(1,1)
, EntityId INT
, [Key] VARCHAR(30)
, Value VARCHAR(100)
)
INSERT INTO @PeopleEav (EntityId, [Key], Value)
VALUES
(1, 'Name', 'Saeed'),
(1, 'Age', '32'),
(1, 'Gender', 'Male'),
(1, 'Key1', 'Value1'),
(1, 'Key2', 'Value2')
SELECT
t.EntityId
, JSON = STUFF((
SELECT ', ' + [Key] + ': "' + Value + '"'
FROM @PeopleEav t2
WHERE t2.EntityId = t2.EntityId
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, '{ ') + ' }'
FROM (
SELECT DISTINCT EntityId
FROM @PeopleEav
) t
--WHERE EntityId = 1
Output -
EntityId JSON
----------- --------------------------------------------------------------------------------------------
1 { Name: "Saeed", Age: "32", Gender: "Male", Key1: "Value1", Key2: "Value2" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With