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

JOLT转换需求:筛选typeName为xx的项并保留指定字段

JOLT转换:过滤typeName为'xx'的项并保留指定字段

需求说明

过滤JSON中所有层级下type.typeName等于xx的项,将符合条件的项整理为数组,且每个项仅保留以下字段:

  • id、action
  • 完整的payment对象
  • type对象仅保留id和typeName
  • details对象仅保留place和status

输入JSON

{
  "id": 11,
  "item": [
    {
      "id": "11_1",
      "action": "add",
      "payment": {
        "paied": true,
        "coin": "dollar"
      },
      "type": {
        "id": "11_1_xx",
        "typeName": "xx",
        "typeGroup": "xx",
        "typeValue": "xx"
      },
      "details": {
        "place": {
          "id": "123",
          "name": "xx"
        },
        "status": {
          "id": "123",
          "name": "xx"
        },
        "reason": {
          "id": "123",
          "name": "xx"
        }
      },
      "item": [
        {
          "id": "11_1_1",
          "action": "add",
          "payment": {
            "paied": true,
            "coin": "dollar"
          },
          "type": {
            "id": "11_1_1_zz",
            "typeName": "zz",
            "typeGroup": "zz",
            "typeValue": "zz"
          },
          "details": {
            "place": {
              "id": "123",
              "name": "xx"
            },
            "status": {
              "id": "123",
              "name": "xx"
            },
            "reason": {
              "id": "123",
              "name": "xx"
            }
          },
          "item": [
            {
              "id": "11_1_1_1",
              "action": "add",
              "payment": {
                "paied": true,
                "coin": "NIS"
              },
              "type": {
                "id": "11_1_1_1_xx",
                "typeName": "xx",
                "typeGroup": "xx",
                "typeValue": "xx"
              },
              "details": {
                "place": {
                  "id": "123",
                  "name": "xx"
                },
                "status": {
                  "id": "123",
                  "name": "xx"
                },
                "reason": {
                  "id": "123",
                  "name": "xx"
                }
              }
            }
          ]
        },
        {
          "id": "11_1_2",
          "action": "add",
          "payment": {
            "paied": false,
            "coin": "dollar"
          },
          "type": {
            "id": "11_1_2_xx",
            "typeName": "xx",
            "typeGroup": "xx",
            "typeValue": "xx"
          },
          "details": {
            "place": {
              "id": "123",
              "name": "xx"
            },
            "status": {
              "id": "123",
              "name": "xx"
            },
            "reason": {
              "id": "123",
              "name": "xx"
            }
          },
          "item": [
            {
              "id": "11_1_2_1",
              "action": "add",
              "payment": {
                "paied": false,
                "coin": "NIS"
              },
              "type": {
                "id": "11_1_2_1_zz",
                "typeName": "zz",
                "typeGroup": "zz",
                "typeValue": "zz"
              },
              "details": {
                "place": {
                  "id": "123",
                  "name": "xx"
                },
                "status": {
                  "id": "123",
                  "name": "xx"
                },
                "reason": {
                  "id": "123",
                  "name": "xx"
                }
              }
            }
          ]
        }
      ]
    }
  ]
}

预期输出JSON

[
  {
    "id": "11_1",
    "action": "add",
    "payment": {
      "paied": true,
      "coin": "dollar"
    },
    "type": {
      "id": "11_1_xx",
      "typeName": "xx"
    },
    "details": {
      "place": {
        "id": "123",
        "name": "xx"
      },
      "status": {
        "id": "123",
        "name": "xx"
      }
    }
  },
  {
    "id": "11_1_1_1",
    "action": "add",
    "payment": {
      "paied": true,
      "coin": "NIS"
    },
    "type": {
      "id": "11_1_1_1_xx",
      "typeName": "xx"
    },
    "details": {
      "place": {
        "id": "123",
        "name": "xx"
      },
      "status": {
        "id": "123",
        "name": "xx"
      }
    }
  },
  {
    "id": "11_1_2",
    "action": "add",
    "payment": {
      "paied": false,
      "coin": "dollar"
    },
    "type": {
      "id": "11_1_2_xx",
      "typeName": "xx"
    },
    "details": {
      "place": {
        "id": "123",
        "name": "xx"
      },
      "status": {
        "id": "123",
        "name": "xx"
      }
    }
  }
]

JOLT Spec实现

[
  // 递归展开所有嵌套的item数组,平铺所有层级的项
  {
    "operation": "shift",
    "spec": {
      "item": {
        "*": {
          "@": "&",
          "item": {
            "*": {
              "@": "&2",
              "item": {
                "*": {
                  "@": "&3"
                }
              }
            }
          }
        }
      }
    }
  },
  // 过滤出type.typeName为xx的项
  {
    "operation": "shift",
    "spec": {
      "*": {
        "type": {
          "typeName": {
            "xx": {
              "@2": "[]"
            }
          }
        }
      }
    }
  },
  // 保留指定字段,裁剪冗余内容
  {
    "operation": "shift",
    "spec": {
      "*": {
        "id": "[&1].id",
        "action": "[&1].action",
        "payment": "[&1].payment",
        "type": {
          "id": "[&2].type.id",
          "typeName": "[&2].type.typeName"
        },
        "details": {
          "place": "[&2].details.place",
          "status": "[&2].details.status"
        }
      }
    }
  }
]

转换说明

  1. 递归平铺贴层:通过多层shift操作,把嵌套在item数组里的所有层级的项提取到同一层级,方便后续统一过滤。
  2. 过滤符合条件的项:匹配type.typeName的值为xx,将对应的完整项筛选出来并存入数组。
  3. 裁剪字段:对每个筛选后的项,只保留需求指定的字段,剔除type里的typeGroup、typeValue以及details里的reason等冗余内容。

内容的提问来源于stack exchange,提问作者Michal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 11:39:26