JOLT 检查值是否为空,保持原样,否则根据条件更新值

问题描述 投票:0回答:1

请提供震动规格 输入json

[
  {
    "name": "Manu",
    "age": 26,
    "location": "New York",
    "risk": "0"
  },
  {
    "name": "Manju",
    "age": 29,
    "location": "New York city",
    "risk": null
  }
]

如果风险为 0,则风险级别为低;如果为空,则风险级别为空;如果风险为 1,则风险级别为高

[
  {
    "name": "Manu",
    "age": 26,
    "location": "New York",
    "risklevel": "Low"
  },
  {
    "name": "Manju",
    "age": 29,
    "location": "New York city",
    "risklevel": null
  }
]

尝试过的规格是:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "name": "[&1].name",
        "age": "[&1].age",
        "location": "[&1].location",
        "risk": {
          "0": {
            "#Low": "[&2].risklevel"
          },
          "1": {
            "#High": "[&2].risklevel"
          },
          "null": null,
          "*": {
            "@(2,risklevel)": "[&2].risklevel"
          }
        },
        "*": "[&1].risklevel"
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "*": {
        "risklevel": "null"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "risklevel": "=toString"
      }
    }
  }
]
json spring jolt
1个回答
0
投票

您可以使用以下转换:

[//set an arbtrary value(say "NUll") to a null valued "risk"
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "~risk": "NUll"//occures whnever risk is null due to the ~ operator 
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[&1].&", //the attributes other than "risk"
        "risk": {
          "1": { "#High": "[&3].riskLevel" },//hardcode by # operator
          "0": { "#Low": "[&3].riskLevel" },
          "NUll": { "@": "[&3].riskLevel" }
        }
      }
    }
  }
]

网站上的 演示 https://jolt-demo.appspot.com/ 是:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.