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

如何编写JOLT Spec实现按corporateFlag移除数组内的ratingValue对象

解决JOLT转换中根据corporateFlag移除ratingValue的问题

需求说明

  • 遍历每个ratingResult对象,检查其corporateFlag字段值
  • 若该值为true,则移除对应ratingResult中的ratingValue对象
  • 其余JSON结构保持不变

输入JSON

{
  "rating": {
    "id": "46787db9-a3fd-4f7f-9bc8-572604c9a267",
    "ratingResponse": {
      "ratingId": "46787db9-a3fd-4f7f-9bc8-572604c9a267",
      "ratingResults": [
        {
          "customerId": "123",
          "corporateFlag": false,
          "decision": {
            "code": "R",
            "description": "Refer"
          },
          "ratingValue": {
            "rating": "AAA",
            "ratingPoints": 123
          },
          "role": "Applicant"
        },
        {
          "customerId": "456",
          "corporateFlag": true,
          "decision": {
            "code": "R",
            "description": "Refer"
          },
          "ratingValue": {
            "rating": "B",
            "ratingPoints": 423
          },
          "role": "Company"
        }
      ]
    },
    "links": []
  }
}

期望输出JSON

{
  "rating": {
    "id": "46787db9-a3fd-4f7f-9bc8-572604c9a267",
    "ratingResponse": {
      "ratingId": "46787db9-a3fd-4f7f-9bc8-572604c9a267",
      "ratingResults": [
        {
          "customerId": "123",
          "corporateFlag": false,
          "decision": {
            "code": "R",
            "description": "Refer"
          },
          "ratingValue": {
            "rating": "AAA",
            "ratingPoints": 123
          },
          "role": "Applicant"
        },
        {
          "customerId": "456",
          "corporateFlag": true,
          "decision": {
            "code": "R",
            "description": "Refer"
          },
          "role": "Company"
        }
      ]
    },
    "links": []
  }
}

尝试过的JOLT Spec(存在问题)

[
  {
    "operation": "shift",
    "spec": {
      "rating": {
        "*": "rating.&",
        "ratingResponse": {
          "*": "rating.ratingResponse.&",
          "ratingResults": {
            "*": {
              "*": "rating.ratingResponse.ratingResults[&1].&",
              "ratingValue": {
                "@(2,corporateFlag)": {
                  "false": {
                    "@0": "rating.ratingResponse.ratingResults[&1].ratingValue"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

问题说明

该规则会无视corporateFlag的值,始终移除ratingValue,得到如下错误结果:

{
  "rating" : {
    "id" : "46787db9-a3fd-4f7f-9bc8-572604c9a267",
    "ratingResponse" : {
      "ratingId" : "46787db9-a3fd-4f7f-9bc8-572604c9a267",
      "ratingResults" : [ {
        "customerId" : "123",
        "corporateFlag" : false,
        "decision" : {
          "code" : "R",
          "description" : "Refer"
        },
        "role" : "Applicant"
      }, {
        "customerId" : "456",
        "corporateFlag" : true,
        "decision" : {
          "code" : "R",
          "description" : "Refer"
        },
        "role" : "Company"
      } ]
    },
    "links" : [ ]
  }
}

错误原因是索引引用错误:在shift操作中,数组元素的索引应该用&0而非&1,导致ratingValue无法被正确映射回目标路径。

正确的JOLT Spec

[
  {
    "operation": "shift",
    "spec": {
      "rating": {
        "*": "rating.&",
        "ratingResponse": {
          "*": "rating.ratingResponse.&",
          "ratingResults": {
            "*": {
              "*": "rating.ratingResponse.ratingResults[&0].&",
              "ratingValue": {
                "@(2,corporateFlag)": {
                  "false": {
                    "@0": "rating.ratingResponse.ratingResults[&2].ratingValue"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

规则说明

  1. 首先完整复制rating下的所有顶级字段(如id、links)以及ratingResponse下的非数组字段(如ratingId)
  2. 遍历ratingResults数组的每个元素:
    • 先复制元素下除ratingValue外的所有字段到目标数组对应位置
    • 针对ratingValue,通过@(2,corporateFlag)向上两层获取当前元素的corporateFlag值:
      • 若值为false,则将ratingValue本身(@0)映射到目标数组对应位置的ratingValue字段
      • 若值为true,则不做映射,ratingValue自然被移除
  3. 所有其他结构保持与输入一致

内容的提问来源于stack exchange,提问作者CC Чебурашка

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 03:24:51