SQL proc 使用来自 Web 服务的 JSON 有效负载,但数据在过程中接收之前已被转义。我有一个这样的有效载荷:
{
"RES_NBR_ID": "00000056566",
"RES_SCRIPT_NM": "Test Script",
"RES_SCRIPT_ID": "jcet2",
"RES_RESULT_GROUP_ID": 42622528,
"RES_CREATED_BY_ID": "Jcccc",
"RES_QUESTION_TXT": "asdf",
"RES_ANSWER_TXT": "asdfa\"asfasf",
"SORT_NBR": 4,
"RES_QUESTION_ID": 4,
"RES_DELETED_BY_ID": ""
}
您会注意到 ANSWER_TXT 确实是 asdfa"asfasf 反斜杠是由服务上的 JSON.stringify 函数放入的
因此,将使用 OPENJSON 如下所示删除所有 stringfy 序列:
SELECT (SELECT MAX(RES_ID) FROM SCRT_RESULTS) + ROW_NUMBER() OVER(ORDER BY RES_QUESTION_TXT) AS RES_ID
,RES_NBR_ID
,RES_SCRIPT_NM
,RES_QUESTION_TXT
,RES_ANSWER_TXT
,RES_CREATED_BY_ID
,getdate() [RES_CREATED_DT]
,RES_DELETED_BY_ID
,getdate() [RES_DELETED_DT]
,RES_RESULT_GROUP_ID
,RES_SORT_NBR
,RES_SCRIPT_ID
,RES_QUESTION_ID
,getUTCdate() [RES_CREATED_UTC_DT]
from openjson(@json_result)
WITH(
RES_NBR_ID varchar(16)
,RES_SCRIPT_NM varchar(150)
,RES_SCRIPT_ID varchar(5)
,RES_RESULT_GROUP_ID bigint
,RES_CREATED_BY_ID varchar(20)
,RES_QUESTION_TXT varchar(2000)
,RES_ANSWER_TXT varchar(2000)
,RES_SORT_NBR int
,RES_QUESTION_ID int
,RES_DELETED_BY_ID varchar(20)
)