简体   繁体   English

如何使用 json_extract() 查询 SQL 中嵌套的 JSON 数据值?

[英]How would I query nested JSON data values in SQL using json_extract()?

Mysql version is 6.7+ Mysql版本为6.7+

Example of the column I need to get a value from below.我需要从下面获取值的列示例。

json_extract(goal_templates.template_data, group_concat('$.resources')) ->This results in a NULL return for all rows. json_extract(goal_templates.template_data, group_concat('$.resources')) ->这会导致所有行的 NULL 返回。

:template_data :template_data
{"Resolve housing issues": {"tasks": [{"name": "Use Decision Map to explore paths to resolving'' housing issue", "end_date": "15 days", "taskType": 3, "help_text": "", "resources": [], "start_date": "today", "actionOrder": "1", "recur_every": "", "resource_reference_id": ""}, {"name": "Select a local Debt & Credit Counseling Service", "end_date": "15 days", "taskType": 3, "help_text": "Add & tag local Credit & Debt Counseling Service (Organization)", "resources": ["14579", "14580"], "start_date": "today", "actionOrder": "2", "recur_every": "", "resource_reference_id": "14579, 14580"}, {"name": "[Schedule Credit & Debt Counseling as SGE or RGE]", "end_date": "15 days", "taskType": 3, "help_text": "", "resources": [], "start_date": "today", "actionOrder": "3", "recur_every": "", "resource_reference_id": ""}, {"name": "Rate resource for benefit of those who follow", "end_date": "15 days", "taskType": 3, "help_text": "", "resources": [], "start_date": "today", "actionOrder": "4", "recur_every": "", "resource_reference_id": ""}], "sequence_num": "1"}}
mysql> select json_extract(template_data, '$."Resolve housing issues".tasks[*].resources') as resources from goal_templates;
+----------------------------------+
| resources                        |
+----------------------------------+
| [[], ["14579", "14580"], [], []] |
+----------------------------------+

We use JSON_EXTRACT to extract a key from the JSON_COLUMN using the following syntax:我们使用JSON_COLUMN使用以下语法从JSON_EXTRACT中提取键:

JSON_EXTRACT(json_field, '$.key')

If, however, we need to extract nested keys like in your case, we can either append the nested child keys to the path like但是,如果我们需要像您的情况一样提取嵌套键,我们可以 append 嵌套子键到路径,例如

JSON_EXTRACT('{"resolve_housing_issues": {"tasks": [{"name": "Decision Map", "end_date": "15 days"}]}}', '$.resolve_housing_issues.tasks[0].name') 

as is depicted in @bill-karwin's answer or make use of the wildcards like the following:如@bill-karwin 的回答中所述,或使用如下通配符:

SELECT JSON_EXTRACT('{"resolve_housing_issues": {"tasks": [{"name": "Decision Map", "end_date": "15 days"}]}}', '$**.resolve_housing_issues') as resolve_housing_issues;

It produces the following result:它产生以下结果:

提取子键的值

While the query而查询

SELECT JSON_EXTRACT('{"resolve_housing_issues": {"tasks": [{"name": "Decision Map", "end_date": "15 days"}]}}', '$**.tasks') as tasks;
produces the following result: 产生以下结果:

提取子键的值

So on and so forth.等等等等。

More on this can be found here .更多信息可以在这里找到。

The selector I was looking for seems to be a "$**" wildcard.我正在寻找的选择器似乎是一个“$**”通配符。 This allowed me to grab all of the unnamed objects for each of the rows and the values of the nested resources.这使我能够获取每一行的所有未命名对象以及嵌套资源的值。

JSON_EXTRACT(template_data, '$**.tasks[*].resources') AS resources

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

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