简体   繁体   English

SQL中的JSON数组-从JSON数组中提取多个值

[英]JSON Array in SQL - Extracting multiple values from JSON array

I have a JSON column [gca] that looks like this 我有一个看起来像这样的JSON列[gca]

[{
    "i":"https://some.image.URL 1",
    "u":"https://some.product.url",
    "n":"Product 1 Name",
    "q":"1",
    "sk":"sku number 1",
    "st":"$499.99"
 },
 {
    "i":"https://some.image.URL 2",
    "u":"https://some.product.url",
    "n":"Product 2 Name",
    "q":"1",
    "sk":"sku number 2",
    "st":"$499.99"
}]

I want to extract values specific to position. 我想提取特定于位置的值。 For example: 例如:

JSON_VALUE ([gca], '$[0].i') + ', ' + JSON_VALUE ([gca], '$[1].i')

So the result would be a string 所以结果将是一个字符串

image url 1, image url 2

I tried the cross apply solution from this answer , but I get this error: 我尝试从此答案进行交叉申请解决方案,但出现此错误:

JSON text is not properly formatted. JSON文本格式不正确。 Unexpected character 'h' is found at position 0 在位置0处发现意外字符'h'

Expected results -- https://i.stack.imgur.com/UaRjQ.png 预期结果-https: //i.stack.imgur.com/UaRjQ.png

Your statement is correct and based on the JSON data in the question, the reason for this "... JSON text is not properly formatted. Unexpected character 'h' is found at position 0' error ... " error is somewhere else. 您的陈述是正确的,并且基于问题中的JSON数据,此原因“ ... JSON文本格式不正确。在位置0'处发现意外字符'h'错误...”错误在其他地方。

Your JSON text is a valid JSON array, so you have two possible approaches to get your expected results: JSON文本是有效的JSON数组,因此您有两种可能的方法来获得预期的结果:

  • if this JSON array has always two items, you should use JSON_VALUE() to access each item 如果此JSON数组始终有两个项目,则应使用JSON_VALUE()访问每个项目
  • if the count of the items is not known, you should use OPENJSON() with additional CROSS APPLY operator and string aggregation function. 如果未知项目数,则应将OPENJSON()与其他CROSS APPLY运算符和字符串聚合函数一起使用。

Table: 表:

CREATE TABLE #Data (
   [gca] nvarchar(max)
)
INSERT INTO #Data
   ([gca])
VALUES
   (N'[{"i":"https://some.image.URL 1","u":"https://some.product.url","n":"Product 1 Name","q":"1","sk":"sku number 1","st":"$499.99"},{"i":"https://some.image.URL 2","u":"https://some.product.url","n":"Product 2 Name","q":"1","sk":"sku number 2","st":"$499.99"}]'),
   (N'[{"i":"https://some.image.URL 1","u":"https://some.product.url","n":"Product 1 Name","q":"1","sk":"sku number 1","st":"$499.99"},{"i":"https://some.image.URL 2","u":"https://some.product.url","n":"Product 2 Name","q":"1","sk":"sku number 2","st":"$499.99"}]')

Statements: 声明:

-- For fixed structure with two items
SELECT JSON_VALUE ([gca], '$[0].i') + ', ' + JSON_VALUE ([gca], '$[1].i') AS [http]
FROM #Data

-- For JSON array with multiple items and SQL Server 2017+ 
SELECT j.*
FROM #Data d
CROSS APPLY (
   SELECT STRING_AGG([http], ',') AS [http]
   FROM OPENJSON(d.[gca]) WITH ([http] varchar(max) '$.i') 
) j

-- For JSON array with multiple items
SELECT STUFF(j.[http], 1, 1, N'') AS [http]
FROM #Data d
CROSS APPLY (
   SELECT CONCAT(',', [http])
   FROM OPENJSON(d.[gca]) WITH ([http] varchar(max) '$.i')
   FOR XML PATH('')
) j([http])

Output: 输出:

-------------------------------------------------
http
-------------------------------------------------
https://some.image.URL 1,https://some.image.URL 2
https://some.image.URL 1,https://some.image.URL 2

Note, that JSON support was intoduced in SQL Server 2016 and STRING_AGG() was introduced in SQL Server 2017. For string aggregation in earlier versions use FOR XML PATH . 请注意,在SQL Server 2016中引入了JSON支持,在SQL Server 2017中引入了STRING_AGG() 。对于早期版本中的字符串聚合,请使用FOR XML PATH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM