【问题标题】:How to convert the below SQL Data to the below given JSON data如何将以下 SQL 数据转换为以下给定的 JSON 数据
【发布时间】:2021-08-08 07:14:19
【问题描述】:

我从 SQL 获取以下数据。

Person  Occupation  Salary  Count
Ajay    Farmer      5000    1
Ajay    Barber      6000    1
Ajay    Carpenter   8000    1
Mahesh  Farmer      3000    1
Mahesh  Barber      8000    1
Mahesh  Carpenter   9000    1

我需要将其转换为以下 JSON 格式:我尝试过,但未能应用适当的组。

{
  "Ajay": [
    {
      "Job Details": [
        {
          "Occupation": "Farmer",
          "Salary": "5000",
          "count": 1
        },
        {
          "Occupation": "Barber",
          "Salary": "6000",
          "count": 1
        },
        {
          "Occupation": "Carpenter",
          "Salary": "8000",
          "count": 1
        }
      ]
    }
  ],
  "Mahesh": [
    {
      "Job Details": [
        {
          "Occupation": "Farmer",
          "Salary": "3000",
          "count": 1
        },
        {
          "Occupation": "Barber",
          "Salary": "8000",
          "count": 1
        },
        {
          "Occupation": "Carpenter",
          "Salary": "9000",
          "count": 1
        }
      ]
    }
  ]
}

【问题讨论】:

  • 请向我们展示如何您将其转换为 JSON。
  • 没有动态 SQL 几乎是不可能的,因为 SQL Server 不支持动态 JSON 键
  • 我尝试创建两个模型。一人模型和其他细节模型。但我坚持自己创建模型。
  • 在提出问题时,您需要提供一个可重现的最小示例。请参考以下链接:stackoverflow.com/help/minimal-reproducible-example 请提供以下内容: (1) DDL 和样本数据填充,即 CREATE table(s) 加上 INSERT T-SQL 语句。 (2) 你需要做什么,即逻辑和你的代码尝试在 T-SQL 中实现它。 (3) 期望的输出,基于上面#1 中的样本数据。 (4) 你的 SQL Server 版本 (SELECT @@version;)

标签: .net sql-server asp.net-mvc entity-framework linq


【解决方案1】:

更新后更符合 OP 的预期结果

通过一些字符串操作

示例

Declare @YourTable Table ([Person] varchar(50),[Occupation] varchar(50),[Salary] int,[Count] int)  Insert Into @YourTable Values 
 ('Ajay','Farmer',5000,1)
,('Ajay','Barber',6000,1)
,('Ajay','Carpenter',8000,1)
,('Mahesh','Farmer',3000,1)
,('Mahesh','Barber',8000,1)
,('Mahesh','Carpenter',9000,1)


 ;with cte as (
 Select Person,JS= (Select [Occupation]=Occupation
                          ,[Salary]    =Salary
                          ,[Count]     =Count
                      From @YourTable where Person=A.Person
                      FOR JSON PATH,Root('Job Details')
                   )
 From @YourTable A
 Group By Person
)
Select '{'+string_agg('"'+string_escape(Person,'json')+'":['+JS+']',',')+'}'
 From  cte

结果

{
    "Ajay": [{
        "Job Details": [{
            "Occupation": "Farmer",
            "Salary": 5000,
            "Count": 1
        }, {
            "Occupation": "Barber",
            "Salary": 6000,
            "Count": 1
        }, {
            "Occupation": "Carpenter",
            "Salary": 8000,
            "Count": 1
        }]
    }],
    "Mahesh": [{
        "Job Details": [{
            "Occupation": "Farmer",
            "Salary": 3000,
            "Count": 1
        }, {
            "Occupation": "Barber",
            "Salary": 8000,
            "Count": 1
        }, {
            "Occupation": "Carpenter",
            "Salary": 9000,
            "Count": 1
        }]
    }]
}

【讨论】:

  • 嗯,我的名字里有",我的用户名里有一个`\`,现在怎么办?
  • @Charlieface 显然,如果存在这样的边缘项目,则需要对其进行转义。
  • 几乎不可能在单个查询(无函数)中使用 正确 转义。这些不是主要的边缘情况,它们无处不在,至少你应该发出健康警告
  • ... +string_escape(Person,'json')+...
  • ..@JohnCappelletti ..当然..select * from openjson('{"a\"\\b":"123"}'); select json_value('{"a\"\\b":"123"}', '$."'+string_escape('a"\b', 'json')+'"')
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-23
  • 2014-03-19
  • 2020-12-18
  • 2021-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多