如何合并数组并为数组项添加键值属性?求Jolt转换方案
Jolt转换规则实现日期属性合并与添加
需求说明
将输入JSON中每个顶层日期键对应的item数组合并为一个数组,同时给每个item对象添加date属性,值为对应的顶层日期键。
Jolt转换规则
[ { "operation": "shift", "spec": { "*": { "item": { "*": { "@2": "date", "@": "[&1]" } } } } }, { "operation": "shift", "spec": { "*": "[]" } } ]
规则解释
- 第一个shift操作:
*匹配所有顶层日期键(如2024-10-10)item定位到每个日期下的数组*遍历数组中的每个对象@2向上回溯两层获取顶层日期键,赋值给date字段@保留原对象的所有属性,存入临时数组[&1](&1表示当前数组索引)
- 第二个shift操作:
- 将临时数组中的所有元素展开到最终的根数组中,完成合并
输入输出验证
- 输入:
{ "2024-10-10": { "item": [ { "attr1": "a", "attr2": "b" }, { "attr1": "c", "attr2": "d" } ] }, "2025-10-10": { "item": [ { "attr1": "e", "attr2": "f" } ] } }
- 输出:
[ { "attr1": "a", "attr2": "b", "date": "2024-10-10" }, { "attr1": "c", "attr2": "d", "date": "2024-10-10" }, { "attr1": "e", "attr2": "f", "date": "2025-10-10" } ]
内容的提问来源于stack exchange,提问作者Steven Sh
相关产品推荐
相关产品推荐

