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