如何从SQL Server中的EAV表创建JSON

时间:2022-06-29 15:26:36

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:

然后可以在SQL Server中使用XML创建一个JSON,如下所示:

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:

这很容易,因为列是预先知道的。但是,我仍然坚持从EAV表创建JSON。例如,如果该表是:

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?

如何创建这个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).

请注意键的数量是可变的(它是一个EAV表)。

2 个解决方案

#1


5  

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" }

#2


1  

If you have Sql Server 2016 you can use FOR JSON. You can also use existing CLR libraries such as JsonSelect or Json4Sql.

如果你有Sql Server 2016,你可以使用JSON。您还可以使用现有的CLR库,如JsonSelect或Json4Sql。

#1


5  

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" }

#2


1  

If you have Sql Server 2016 you can use FOR JSON. You can also use existing CLR libraries such as JsonSelect or Json4Sql.

如果你有Sql Server 2016,你可以使用JSON。您还可以使用现有的CLR库,如JsonSelect或Json4Sql。