Как я могу сгенерировать вложенный вывод json, используя FOR JSON PATH из таблицы с ключом-значением в SQL Server?

Таблица, содержащая ключ-значение, также имеет ParentIDстолбец. Этот столбец содержит идентификатор своего родителя. Я хочу получить вложенный json, используя их.

Таблица "ключ-значение":

      create table #temp
(
    [Id] int, 
    [Key] nvarchar(100),
    [Value] nvarchar(max),
    [ParentId] int
)


insert into #temp select 1,'help','',null
insert into #temp select 2,'info','',1
insert into #temp select 3,'contact','example',1
insert into #temp select 4,'SSS','',1
insert into #temp select 5,'title','example',2
insert into #temp select 6,'text','example',2
insert into #temp select 7,'title','example',4
insert into #temp select 8,'text','',4
insert into #temp select 9,'0','',8
insert into #temp select 10,'1','',8
insert into #temp select 11,'title','example',9
insert into #temp select 12,'text','example',9
insert into #temp select 13,'title','example',10
insert into #temp select 14,'text','example',10

Желаемый результат:

      {
  "help": {
    "info": {
      "title": "example",
      "text": "example"
    },
    "contact": "example",
    "SSS": {
      "title": "example",
      "text": [
        {
          "title": "example",
          "text": "example"
        },
        {
          "title": "example",
          "text": "example"
        }
      ]
    }
  }
}

0 ответов

Другие вопросы по тегам