You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 00:32:41