如何修改Jolt Spec将根节点category加入attributes数组?
Jolt Transform:将根节点category字段并入attributes数组
输入JSON
{ "id": 12345, "category": 2, "productAttributes": { "data": [ { "name": "Size", "slug": "size", "value": "10" }, { "name": "Color", "slug": "color", "value": "Grey" } ] } }
现有Jolt Spec
[ { "operation": "shift", "spec": { "productAttributes": { "data": { "*": { "slug": "attributes[#2].key", "value": "attributes[#2].value", "name": "attributes[#2].name" } } } } } ]
期望输出
{ "attributes": [ { "key": "size", "value": "10", "name": "Size" }, { "key": "color", "value": "Grey", "name": "Color" }, { "key": "category", "value": "2", "name": "Category" } ] }
修改后的Jolt Spec
[ { "operation": "shift", "spec": { "productAttributes": { "data": { "*": { "slug": "attributes[#2].key", "value": "attributes[#2].value", "name": "attributes[#2].name" } } }, "category": { "$": "attributes[].value", "#category": "attributes[].key", "#Category": "attributes[].name" } } } ]
说明
- 新增根节点
category的映射规则,通过$引用原字段的值作为value; - 用
#指定静态字符串作为key和name; - 使用
attributes[]自动将新元素追加到数组末尾,无需手动指定索引,适配原数组元素数量变化的场景。
内容的提问来源于stack exchange,提问作者Enes
相关产品推荐
相关产品推荐

