简体   繁体   English

SQLite JSON_EXTRACT 数组中 1 个对象的所有值

[英]SQLite JSON_EXTRACT All values of 1 object in an array

I have a column containing JSON arrays.我有一列包含 JSON 数组。 The arrays are like this (table users, column user_info):数组是这样的(表用户,列 user_info):

[
 {"email":"xyz@hotmail.com","user_key":"987654","name":"John Paul"},
 {"email":"abc@hotmail.com","user_key":"123456","name":"Tom Sawyer"},
 {"email":"1234@msn.com","user_key":"887645","name":"Bart Simpson"}
]

Some have 3 objects in each array, some have 20, some in between.有些在每个数组中有 3 个对象,有些有 20 个,有些介于两者之间。 I want to pull each "name" value from the arrays.我想从数组中提取每个“名称”值。 So with the above example I want my query results to show:所以在上面的例子中,我希望我的查询结果显示:

John Paul
Tom Sawyer
Bart Simpson

I can do that with:我可以这样做:

SELECT json_extract(users.user_info, '$[0]."name"', $[1]."name"', $[2]."name"')
FROM users

However, if I want to select ALL of them, no matter the length in each row of that column, how would I go about that so I don't have to list out each object number of the array?但是,如果我想选择所有这些,无论该列的每一行的长度如何,我将如何处理,这样我就不必列出数组的每个对象编号?

You need the table-valued function json_each() :您需要表值函数json_each()

SELECT json_extract(json_each.value, '$.name') name
FROM users u, json_each(user_info)

If you want all the names of each row in a comma separated list you could use GROUP_CONCAT() :如果您想要逗号分隔列表中每行的所有名称,您可以使用GROUP_CONCAT()

SELECT GROUP_CONCAT(json_extract(json_each.value, '$.name')) name
FROM users u, json_each(user_info)
GROUP BY u.thread_id

See the demo .请参阅演示

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

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