如何通过JOLT提取JSON数组值并关联id与date字段?
解决JOLT关联id、date与goals字段的问题
原始输入JSON
[ { "id": 107110685, "rows": [ { "date": "2022-07-05", "goals": [] }, { "date": "2022-07-06", "goals": [] } ], "total": { "goals": [] } }, { "id": 107111205, "rows": [ { "date": "2022-07-15", "goals": [ { "conversion_type": "postclick", "cpa": "318.9" }, { "conversion_type": "postview", "cpa": "79.73" } ] }, { "date": "2022-07-16", "goals": [ { "conversion_type": "postview", "cpa": "23.61" } ] } ], "total": { "goals": [ { "conversion_type": "postview", "cpa": "42.77" }, { "conversion_type": "postclick", "cpa": "363.55" } ] } } ]
当前失效的JOLT Spec
[ { "operation": "shift", "spec": { "*": { "rows": { "*": { "goals": { "*": "[]" } } } } } } ]
修改后的JOLT Spec
[ { "operation": "shift", "spec": { "*": { "id": "&1.rows.*.goals.*.id", "rows": { "*": { "date": "&2.&1.goals.*.date", "goals": { "*": { "*": "&3.&2.&1.&" } } } } } } }, { "operation": "shift", "spec": { "*": { "rows": { "*": { "goals": { "*": "[]" } } } } } } ]
逻辑说明
- 第一个
shift操作:把顶级对象的id和日期行的date,向下传递到每个goals对象的层级,和conversion_type、cpa字段合并 - 第二个
shift操作:把所有带完整字段的goals元素,统一收集到一个顶级数组中 - 空的
goals数组会自动被忽略,因为"*"匹配不到任何元素,正好符合需求
目标输出
[ { "id": 107111205, "date": "2022-07-15", "conversion_type": "postclick", "cpa": "318.9" }, { "id": 107111205, "date": "2022-07-15", "conversion_type": "postview", "cpa": "79.73" }, { "id": 107111205, "date": "2022-07-16", "conversion_type": "postview", "cpa": "23.61" } ]
内容的提问来源于stack exchange,提问作者fujidaon
相关产品推荐
相关产品推荐

