mysql - JSON_TABLE - how to access fields inside escaped quotes? - Stack Overflow

I've got a large JSON field that I'm trying to parse out using JSON_TABLE.About half the da

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
Add a comment  | 

1 Answer 1

Reset to default 0

Since 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742346322a4426615.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信