简体   繁体   English

从 MySQL 中的 JSON 对象中提取密钥对值

[英]Extract key-pair values from JSON objects in MySQL

From MySQL JSON data field, I'm extracting data from array like so:从 MySQL JSON 数据字段中,我从数组中提取数据,如下所示:

SELECT 
data ->> '$.fields[*]' as fields 
FROM some_database...

which returns:返回:

[{
"id": 111056, 
"hint": null, 
"slug": "email", 
"label": "E-mail", 
"value": null, 
"field_value": "test@example.com", 
"placeholder": null
}, {
"id": 111057, 
"hint": null, 
"slug": "name", 
"label": "Imię", 
"value": null, 
"field_value": "Aneta", 
"placeholder": null
}]

I can also extract single column:我还可以提取单列:

SELECT 
data ->> '$.fields[*].field_value' as fields 
FROM some_database...

and that returns the following result:并返回以下结果:

[test@example.com, Aneta]

But how can I extract field_value alongside with label as key-pairs?但是如何将field_value label提取为密钥对?

Preferred output would be a single multi-row string containing pairs:首选 output 将是包含对的单个多行字符串:

label: field_value
label: field_value
...

Using example shown above it would get me following output:使用上面显示的示例,它会让我关注 output:

E-mail: test@example.com
Imię: Aneta

One-liner preferred as I have multiple of such arrays to extract from various fields.首选单线,因为我有多个这样的 arrays 可以从各个领域中提取。

Here's an example of extracting the key names as rows:这是将键名提取为行的示例:

select j.keyname from some_database 
cross join json_table(
  json_keys(data->'$[0]'), 
  '$[*]' columns (
    keyname varchar(20) path '$'
  )
) as j;

Output: Output:

+-------------+
| keyname     |
+-------------+
| id          |
| hint        |
| slug        |
| label       |
| value       |
| field_value |
| placeholder |
+-------------+

Now you can join that to the values:现在您可以将其加入值:

select n.n, j.keyname,
  json_unquote(json_extract(f.data, concat('$[', n.n, ']."', j.keyname, '"'))) as value
from some_database as d
cross join json_table(
  json_keys(d.data->'$[0]'),
  '$[*]' columns (
    keyname varchar(20) path '$'
  )
) as j
cross join n
join some_database as f on n.n < json_length(f.data);

Output: Output:

+---+-------------+------------------+
| n | keyname     | value            |
+---+-------------+------------------+
| 0 | id          | 111056           |
| 0 | hint        | null             |
| 0 | slug        | email            |
| 0 | label       | E-mail           |
| 0 | value       | null             |
| 0 | field_value | test@example.com |
| 0 | placeholder | null             |
| 1 | id          | 111057           |
| 1 | hint        | null             |
| 1 | slug        | name             |
| 1 | label       | Imię             |
| 1 | value       | null             |
| 1 | field_value | Aneta            |
| 1 | placeholder | null             |
+---+-------------+------------------+

I'm using a utility table n which is just filled with integers.我正在使用一个只填充整数的实用程序表n

create table n (n int primary key);
insert into n values (0),(1),(2),(3)...;

If this seems like a lot of complex work, then maybe the lesson is that storing data in JSON is not easy, when you want SQL expressions to work on the discrete fields within JSON documents.如果这看起来像很多复杂的工作,那么当您希望 SQL 表达式在 Z0ECD11C1D7A287401D148A23BBD7 文档中的离散字段上工作时,可能的教训是在 JSON 中存储数据并不容易。

You can use JSON_VALUE :您可以使用JSON_VALUE

select JSON_VALUE (json_value_col, '$.selected_key') as selected_value from user_details; select JSON_VALUE (json_value_col, '$.selected_key') 作为来自 user_details 的 selected_value;

You can also use JSON_EXTRACT :您还可以使用JSON_EXTRACT

select JSON_EXTRACT (json_value_col, '$.selected_key') as selected_value from user_details; select JSON_EXTRACT (json_value_col, '$.selected_key') 作为来自 user_details 的 selected_value;

For more details refer: https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html有关详细信息,请参阅: https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html

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

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