简体   繁体   English

在mysql 5.7中从json数组中提取密钥

[英]Extracting keys from json array in mysql 5.7

I have a table with json column. 我有一个带有json列的表。 Below is the example:table employee 下面是示例:表员工

id emp_json
1 {"details" : [{"10":"909"},{"11":"910"}]}
2 {"details" : [{"20":"809"},{"21":"810"}]}

Now i want to extract the ids from the json array 现在我想从json数组中提取ID

I have tried with below query. 我尝试了以下查询。

SELECT JSON_KEYS(JSON_EXTRACT(u.emp_json, '$.details'), '$[0]') FROM employee u ;

but this is giving me only one key in the result which is from the first object of array. 但这只给我一个来自数组第一个对象的键。 As per my understanding this is beacuase of $[0] 据我了解,这是因为$ [0]

Expected is 预期是

id json_key
1  10
1  11
2  20
2  21

Thanks in advance 提前致谢

This will give you comma separated, you are selecting only the index 0 , you should select 1 这将使您以逗号分隔,仅选择索引0,应选择1

SELECT
id,
CONCAT(
   JSON_KEYS(JSON_EXTRACT(u.emp_json, '$.details'), '$[0]'),
   ",",
   JSON_KEYS(JSON_EXTRACT(u.emp_json, '$.details'), '$[1]')
) As json_key
FROM employee u ;

Output :- 输出:-

| id  | json_key      |
| --- | ------------- |
| 1   | ["10"],["11"] |
| 2   | ["20"],["21"] |

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

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