Presto:JSON 提取多层

Presto: JSON extract multiple layers

我试图在每项服务下提取接受检查的名称和状态(多个)。我试过了

TRANSFORM(CAST(JSON_PARSE(json_format(json_extract(json_extract(services_data, '$.services'), '$.inspections'))) AS ARRAY<JSON>), x -> JSON_EXTRACT_SCALAR(x, '$.title')) AS inspection,

有办法吗?我检查了其他问题贴上的一样,但是看不到这些复杂的层。

我获取了您的数据,向其中添加了更多内容以进行测试,并提出了这个 SQL 查询:

with example(record) as (
    values(json '{"services":[{"category_id":1,"completed":true,"created_at":"2020-01-27T20:44:57Z","hazmats":[],"inspections":[{"created_at":"2020-01-27T20:44:57Z","detail":null,"inspection_id":10209887,"name":"Accelerator Pedal","state":"green","updated_at":"2020-01-27T20:44:57Z"},{"created_at":"2020-01-27T20:44:57Z","detail":null,"inspection_id":10209887,"name":"Accelerator Pedal 2","state":"orange","updated_at":"2020-01-27T20:44:57Z"}]},{"category_id":1,"completed":true,"created_at":"2020-01-27T20:44:57Z","hazmats":[],"inspections":[{"created_at":"2020-01-27T20:44:57Z","detail":null,"inspection_id":10209887,"name":"Some dummy name","state":"navy","updated_at":"2020-01-27T20:44:57Z"}]}]}')
)

select name, "state"
from example
cross join unnest(cast(json_extract(record, '$.services') as array<json>)) as u(service)
cross join unnest(cast(json_extract(service, '$.inspections') as array(row(name varchar, "state" varchar)))) as x(name,"state");

结果如下所示:

name                |   state
------------------------------
Accelerator Pedal   |   green
Accelerator Pedal 2 |   orange
Some dummy name     |   navy

在我看来,这正是您所追求的。