[英]Using JSON_TABLE to convert an ARRAY into Columns
I have a database column (named "details") formatted as a JSON object that contains the following data:我有一个格式为 JSON 对象的数据库列(名为“详细信息”),其中包含以下数据:
'{"300-000012": {"is_complete": "False", "is_in_progress": "True"},
"300-000018": {"is_complete": "True", "is_in_progress": "False"}}'
I can't seem to convert the Array into Columns.我似乎无法将数组转换为列。 I've tried
我试过了
SELECT mh.*, jt.*
FROM history AS mh,
JSON_TABLE (mh.details, '$[*]'
COLUMNS (
NESTED PATH '$.*' COLUMNS (jt_complete VARCHAR(255) PATH '$.is_complete'),
NESTED PATH '$.*' COLUMNS (jt_progress VARCHAR(255) PATH '$.is_in_progress')
)
) AS jt)
But I get an Error Code但我收到一个错误代码
Error Code: 3143. Invalid JSON path expression
Ideally I would get something like:理想情况下,我会得到类似的东西:
details jt_complete jt_progress
300-000012 FALSE TRUE
300-000018 TRUE FALSE
Any help would be appreciated.任何帮助,将不胜感激。 Thx
谢谢
This is a tricky one because the keys of the object are variable.这是一个棘手的问题,因为对象的键是可变的。 This means you need to extract the keys and the values separately for each object.
这意味着您需要为每个对象分别提取键和值。 The values can be connected by using an ordinality column for each
JSON_TABLE
and joining them on that:可以通过为每个
JSON_TABLE
使用一个序数列并在其上加入它们来连接这些值:
SELECT mh.id, jk.details, jt.jt_complete, jt.jt_progress
FROM history mh
JOIN JSON_TABLE(
JSON_KEYS(mh.details),
'$[*]' COLUMNS (
rn FOR ORDINALITY,
details VARCHAR(10) PATH '$'
)
) jk
JOIN JSON_TABLE(
JSON_EXTRACT(mh.details, '$.*'),
'$[*]' COLUMNS (
rn FOR ORDINALITY,
jt_complete VARCHAR(10) PATH '$.is_complete',
jt_progress VARCHAR(10) PATH '$.is_in_progress'
)
) jt ON jt.rn = jk.rn
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.