【问题标题】:Parsing JSON in SQL Server with JSON attribute having a number in its name在 SQL Server 中使用名称中包含数字的 JSON 属性解析 JSON
【发布时间】:2021-03-16 07:17:08
【问题描述】:

我正在尝试解析存储在 SQL 服务器表的列中的 JSON 文件。我正在使用JSON_VALUE 函数。但是这个函数不喜欢属性(称为 6Months)的名称中有数字。由于这个原因,它给了我一个错误。有没有办法强制函数读取文件?请参阅下面的查询和错误消息。我非常感谢任何帮助。

Select 
JSON_VALUE(JsonFile, '$.company.6Months.count') 
From dbo.Filestore

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

【问题讨论】:

标签: json sql-server


【解决方案1】:

您需要在 path 表达式中使用引号。正如documentation 中提到的,如果键名以美元符号开头或包含空格等特殊字符,请用引号将其括起来

SELECT JSON_VALUE(JsonFile, '$."company.6Months.count"') 
FROM dbo.Filestore

【讨论】:

  • 非常感谢 Zhorov,为我解决了这个问题。
  • @Stephen 您应该考虑更改该名称。如果你使用点符号,即使是 JavaScript 也会在 6Months 上窒息,即 myObject.6Months
猜你喜欢
  • 1970-01-01
  • 2021-08-31
  • 2021-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 2021-12-13
相关资源
最近更新 更多