在T-SQL OPENJSON查询中转义字符 [英] Escaping characters in T-SQL OPENJSON queries

查看:139
本文介绍了在T-SQL OPENJSON查询中转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下JSON数据

DECLARE @jsonData NVARCHAR(MAX)
SET @jsonData =
'{  
   "insertions":[  
      {  
         "id":"58735A79-DEA8-462B-B3EB-C2797CA9D44E",
         "last-modified":"2017-08-08 13:07:32",
         "label":"HelloWorld1"
      },
      {  
         "id":"00565BCD-4240-46CF-A48F-849CB5A8114F",
         "last-modified":"2017-08-08 13:11:38",
         "label":"HelloWorld12"
      }
   ]
}'

并尝试从中执行选择:

SELECT
    *
FROM
    OPENJSON(JSON_QUERY(@jsonData,'$.insertions'))
WITH
    (uuid UNIQUEIDENTIFIER '$.id',
    modified DATETIME '$.last-modified',
    Label NVARCHAR(128) '$.label'
    )

它不喜欢最后修改字段中的破折号。

It doesn't like the dash in the last-modified field.


Msg 13607,Level 16,St第4行第18行

JSON路径格式不正确。在位置6处发现意外字符'-'。

Msg 13607, Level 16, State 4, Line 18
JSON path is not properly formatted. Unexpected character '-' is found at position 6.

是否有一种方法可以避免查询中的破折号?

Is there a way to escape the dash in the query? Everything works fine if there is no dash.

根据支持JSON的要求,我正在使用兼容级别= 130的SQL Server 2016

As required to support JSON, I'm using SQL Server 2016 with compatibility level = 130

推荐答案

在字段名周围添加双引号似乎可行

Adding double quotes around the field name seems to work

SELECT
    *
FROM
    OPENJSON(JSON_QUERY(@jsonData,'$.insertions'))
WITH
    (uuid UNIQUEIDENTIFIER '$.id',
    modified DATETIME '$."last-modified"',
    Label NVARCHAR(128) '$.label'
    )

这篇关于在T-SQL OPENJSON查询中转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆