简体   繁体   English

如何使用SQL查询访问JSON对象列表或数组中的字段

[英]How to access a field inside a List or Array of JSON Object using SQL query

Consider the following table 考虑下表

+-------------+--------------------------------------------------------------------------+
|company_name |                         products                                         |   
+-------------+--------------------------------------------------------------------------+
|   comp1     | [{"name": "prod1","pending": false}, {"name": "prod2","pending": true}]  |
+-------------+--------------------------------------------------------------------------+

Suppose I want to retrieve results based on the the value of "name" field inside "products" column, for ex: if "pending" = "true", then display the company_name and also the product name of the particular JSON object inside the List or Array. 假设我想根据“产品”列中“名称”字段的值来检索结果,例如:如果“ pending” =“ true”,则显示company_name以及内部特定JSON对象的产品名称列表或数组。

Result should look something like, 结果应该像这样

+-------------+-------------------------------+
|company_name |           product_name        |
+-------------+-------------------------------+
| comp1       |            prod2              | 
+-------------+-------------------------------+

您应该使用python,perl等程序来检索json并将其转换为array,然后打印所需的array列。

use index to get value $[ index ] 使用索引获取值$ [index]

Code will be like this: 代码将如下所示:

Select company_name,json_extract(products,'$[1].name') product_name from table where json_extract(products,'$[1].pending')='false' ;

Note: create a dummy table with numbers and join it for index 注意:用数字创建一个虚拟表并将其加入索引

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

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