Jolt嵌套JSON转换输出异常:需保留完整对象字段
Jolt过滤JSON并保留完整对象的解决方案
需求说明
过滤输入JSON数组中address.status等于Active的对象,要求保留对象的完整结构(包含firstname、lastname、id、address和department所有字段)。
输入JSON
[ { "firstname": "ABC", "lastname": "PQR", "id": 1, "address": { "city": "LA", "zipcode": "123", "addressType": "Home", "status": "Active" }, "department": { "id": "12", "manager": "tyx", "extraInfo": "N/A" } }, { "firstname": "MNO", "lastname": "LLL", "id": 2, "address": { "city": "TX", "zipcode": "234", "addressType": "Home", "status": "Active" }, "department": { "id": "123", "manager": "tyxx", "extraInfo": "N/A" } } ]
现有Jolt规格(问题版本)
[ { "operation": "shift", "spec": { "*": { "address": { "status": { "Active": { "@2": "[]" } } } } } } ]
问题:使用@2仅引用到address层级的对象,导致输出只包含address相关字段。
修正后的Jolt规格
[ { "operation": "shift", "spec": { "*": { "address": { "status": { "Active": { "@3": "[]" } } } } } } ]
规则说明
*:遍历输入数组中的每个元素address.status.Active:精准匹配address下status为Active的条件@3:从当前Active节点向上追溯3层,获取包含firstname、lastname等字段的完整对象(层级关系:Active→status→address→ 外层完整对象)"[]":将符合条件的对象收集到输出数组中
输出结果
[ { "firstname": "ABC", "lastname": "PQR", "id": 1, "address": { "city": "LA", "zipcode": "123", "addressType": "Home", "status": "Active" }, "department": { "id": "12", "manager": "tyx", "extraInfo": "N/A" } }, { "firstname": "MNO", "lastname": "LLL", "id": 2, "address": { "city": "TX", "zipcode": "234", "addressType": "Home", "status": "Active" }, "department": { "id": "123", "manager": "tyxx", "extraInfo": "N/A" } } ]
内容的提问来源于stack exchange,提问作者Dot Net Dev 19
相关产品推荐
相关产品推荐

