Как заменить строку на int в значении JSON в столбце базы данных? - PullRequest
0 голосов
/ 15 ноября 2018

Я использую ответ по умолчанию для пользователя в моем проекте в соответствии со следующей таблицей.

Таблица: ПОЛЬЗОВАТЕЛИ | ID | ИМЯ | DEFAULTRESPONSE

Ответ по умолчанию содержит строку в формате JSON. Мне нужно изменить месяц и день всех родственных_дней с строки на int.

Вот пример предыдущего json:

[
 {
  "id":1,
  "relatives_birthdays":[ 
  { 
    "month":"8",
    "day": "1",
    "description": "Birthday mother"
  }, 
  { 
    "month":"3",
    "day": "27",
    "description": "Birthday brother"
  }, 
  { 
    "month":"4",
    "day": "12",
    "description": "Birthday father"
  }
  ]
 },
 {
  "id":2,
  "relatives_birthdays":[ 
  { 
    "month":"12",
    "day": "11",
    "description": "Birthday mother"
  }, 
  { 
    "month":"1",
    "day": "2",
    "description": "Birthday brother"
  }, 
  { 
    "month":"7",
    "day": "18",
    "description": "Birthday father"
  }
  ]
 }
]

А вот нужный мне json:

[
 {
  "id":1,
  "relatives_birthdays":[ 
  { 
    "month": 8,
    "day": 1,
    "description": "Birthday mother"
  }, 
  { 
    "month": 3,
    "day": 27,
    "description": "Birthday brother"
  }, 
  { 
    "month": 4,
    "day": 12,
    "description": "Birthday father"
  }
  ]
 },
 {
  "id":2,
  "relatives_birthdays":[ 
  { 
    "month": 12,
    "day": 11,
    "description": "Birthday mother"
  }, 
  { 
    "month": 1,
    "day": 2,
    "description": "Birthday brother"
  }, 
  { 
    "month": 7,
    "day": 18,
    "description": "Birthday father"
  }
  ]
 }
]

Какие-нибудь идеи относительно сценария, которые мне нужно выполнить, чтобы выполнить это?

1 Ответ

0 голосов
/ 16 ноября 2018

Вы можете проанализировать json с помощью openjson и затем восстановить его. Это будет работать, но, вероятно, очень неэффективно:

declare @j nvarchar(max) = '[
 {
  "id":1,
  "relatives_birthdays":[ 
  { 
    "month":"8",
    "day": "1",
    "description": "Birthday mother"
  }, 
  { 
    "month":"3",
    "day": "27",
    "description": "Birthday brother"
  }, 
  { 
    "month":"4",
    "day": "12",
    "description": "Birthday father"
  }
  ]
 },
 {
  "id":2,
  "relatives_birthdays":[ 
  { 
    "month":"12",
    "day": "11",
    "description": "Birthday mother"
  }, 
  { 
    "month":"1",
    "day": "2",
    "description": "Birthday brother"
  }, 
  { 
    "month":"7",
    "day": "18",
    "description": "Birthday father"
  }
  ]
 }
]'

select rt.[id]
 , relatives_birthdays.[month]
 , relatives_birthdays.[day]
 , relatives_birthdays.[description]
from (
 select p.id
 from openjson(@j) with (id int) p
 ) as rt
inner join (
 select p.id
  , c.month
  , c.day
  , c.description
 from openjson(@j) with (
   id int
   , relatives_birthdays nvarchar(max) as json
   ) p
 cross apply openjson(relatives_birthdays) with (
   month int
   , day int
   , description nvarchar(max)
   ) c
 ) as relatives_birthdays
 on rt.id = relatives_birthdays.id
for json auto

Это результирующий JSON:

[
  {
    "id": 1,
    "relatives_birthdays": [
      {
        "month": 8,
        "day": 1,
        "description": "Birthday mother"
      },
      {
        "month": 3,
        "day": 27,
        "description": "Birthday brother"
      },
      {
        "month": 4,
        "day": 12,
        "description": "Birthday father"
      }
    ]
  },
  {
    "id": 2,
    "relatives_birthdays": [
      {
        "month": 12,
        "day": 11,
        "description": "Birthday mother"
      },
      {
        "month": 1,
        "day": 2,
        "description": "Birthday brother"
      },
      {
        "month": 7,
        "day": 18,
        "description": "Birthday father"
      }
    ]
  }
]
...