admin管理员组文章数量:1333619
I've got a large JSON field that I'm trying to parse out using JSON_TABLE. About half the data is straightforward, like:
"run_summary":{"num_records":6,"num_runtime_errors":0,"num_checkpoint_failed":4,"num_files":4}
But the other half contains escaped quotes, such as:
"current_review_id":"{\"value\": \"b13d2f43-7abb-40f8-9867-6349c0c84c47\", \"recorded_timestamp\": 1696865721868}"
I have no trouble breaking out the columns from the run_summary, i.e.
num_records int PATH '$.run_summary.num_records'
However, I can't figure out how to get at the ones inside the escaped quotes...this does not work, for example:
value varchar(40) PATH '$.flow_job_metrics.current_review_id.value'
...which just returns null.
I have tried putting additional quotes inside the path, like:
'$.flow_job_metrics.current_review_id."value"'
and other crazy things like:
'$.flow_job_metrics.current_review_id.\"value\"'
but these either result in syntax errors or more null values.
Can anyone help me figure out how to get at these?
I've got a large JSON field that I'm trying to parse out using JSON_TABLE. About half the data is straightforward, like:
"run_summary":{"num_records":6,"num_runtime_errors":0,"num_checkpoint_failed":4,"num_files":4}
But the other half contains escaped quotes, such as:
"current_review_id":"{\"value\": \"b13d2f43-7abb-40f8-9867-6349c0c84c47\", \"recorded_timestamp\": 1696865721868}"
I have no trouble breaking out the columns from the run_summary, i.e.
num_records int PATH '$.run_summary.num_records'
However, I can't figure out how to get at the ones inside the escaped quotes...this does not work, for example:
value varchar(40) PATH '$.flow_job_metrics.current_review_id.value'
...which just returns null.
I have tried putting additional quotes inside the path, like:
'$.flow_job_metrics.current_review_id."value"'
and other crazy things like:
'$.flow_job_metrics.current_review_id.\"value\"'
but these either result in syntax errors or more null values.
Can anyone help me figure out how to get at these?
Share Improve this question edited Nov 20, 2024 at 16:05 Bill Karwin 563k87 gold badges702 silver badges861 bronze badges asked Nov 20, 2024 at 16:05 user28384901user28384901 132 bronze badges 1- But the other half contains escaped quotes Please provide complete CREATE TABLE. It seems that you use not JSON but TEXT column datatype. Also provide 1-2 data rows in INSERT INTO form. Tips for asking a good Structured Query Language (SQL) question, #5 and #3 – Akina Commented Nov 20, 2024 at 17:29
1 Answer
Reset to default 0Since the JSON is actually double-encoded, that is, part of your JSON is just a string, which is part of the "outer" JSON document, you'll have to double-decode it:
create table mytable (
id serial primary key,
data json
);
insert into mytable set data = '{"current_review_id":"{\\"value\\": \\"b13d2f43-7abb-40f8-9867-6349c0c84c47\\", \\"recorded_timestamp\\": 1696865721868}"}';
select id, j1.current_review_id, j2.value, j2.recorded_timestamp from mytable
cross join json_table(data, '$' columns (
current_review_id json path '$.current_review_id'
)) as j1
cross join json_table(json_unquote(j1.current_review_id), '$' columns (
value varchar(36) path '$.value',
recorded_timestamp bigint path '$.recorded_timestamp'
)) as j2;
Output, tested on MySQL 8.4.3:
*************************** 1. row ***************************
id: 1
current_review_id: "{\"value\": \"b13d2f43-7abb-40f8-9867-6349c0c84c47\", \"recorded_timestamp\": 1696865721868}"
value: b13d2f43-7abb-40f8-9867-6349c0c84c47
recorded_timestamp: 1696865721868
本文标签: mysqlJSONTABLEhow to access fields inside escaped quotesStack Overflow
版权声明:本文标题:mysql - JSON_TABLE - how to access fields inside escaped quotes? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742346322a2457593.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论