JSON_MODIFY 在“/”前面添加额外的“\”

Ayu*_*yal 7 sql t-sql sql-server json

我正在尝试使用 SQL Server 中内置的 JSON 函数在现有 JSON 中插入一个值。

现有的 JSON:

{
  "array": [
    {
      "type": "_api",
      "content": {
        "acId": "sometext/567890"
      }
    }
  ]
}


SET @JSONData = JSON_MODIFY(@JSONData,'$.array[0].content.uId ', 'sometext/1234/locations/1234')  
Run Code Online (Sandbox Code Playgroud)

但是插入 JSON 的结果值如下所示。

我需要它来, sometext/1234/locations/1234但它被添加为sometext\/1234\/locations\/1234

{
  "array": [
    {
      "type": "_api",
      "content": {
        "acId": "sometext/567890",
        "uId": "sometext\/1234\/locations\/1234"
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

小智 1

将 JSON_MODIFY 与替换函数封装为不正确的字符:

SET @JSONData = REPLACE(JSON_MODIFY(@JSONData,'$.array[0].content.uId', 'sometext/1234/locations/1234'), '\/', '/')
Run Code Online (Sandbox Code Playgroud)

  • 您应该使用 `"\/"` 而不是 `"\\/"` (2认同)