json JOLT根据调用成功与否应用不同的转换

1mrurvl1  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(110)

我正在调用一个rest web服务,收到了以下响应,具体取决于调用是否成功。

输入1:

{
  "success": true,
  "data": {
    "series": "XX/32/V32/LM",
    "number": 242,
    "end_date": "31/12/2023",
    "premium": "2309.68",
    "premium_net": "2286.58",
    "total_premium": 2494.46,
    "commission": 1,
    "installments": [
      {
        "number": 1,
        "due_date": "30/12/2022",
        "value": "2494.46",
        "currency": "RON"
      }
    ],
    "reference_premium": 1061,
    "direct_settlement_cover": 1,
    "direct_settlement": 184.78,
    "direct_settlement_net": 182.93,
    "bm": "B0",
    "exclusion_countries": [
      "BY",
      "IL",
      "IR",
      "MA",
      "RUS",
      "TN",
      "UA"
    ]
  },
  "message": "Polița a fost emisă cu succes"
}

输入2:

{
  "success": false,
  "message": "Eroare validare date",
  "data": {
    "pay_document": [
      "Trebuie să menționați modalitatea de plată"
    ]
  }
}

我编写了以下单独工作的操作

成功规格:

[
  {
    "operation": "shift",
    "spec": {
      "message": "body.info_message",
      "data": {
        "#1": "body.good_id",
        "series": "body.policy_series",
        "number": "body.policy_number",
        "end_date": "body.policy_end_date",
        "direct_settlement_cover": {
          "1": {
            "#Decontare directa": "clauses[0].clause_name",
            "#1": "clauses[0].clause_id"
          }
        },
        "direct_settlement": "clauses[0].premium",
        "installments": {
          "*": {
            "number": "body.installments[&1].number",
            "value": "body.installments[&1].amount",
            "currency": "body.installments[&1].currency",
            "due_date": "body.installments[&1].due_date"
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "body": {
        "good_id": "=toInteger"
      }
    }
  }
]

错误规范:

[
  {
    "operation": "shift",
    "spec": {
      "message": "body.info_message",
      "data": {
        "*": {
          "*": "body.error_message"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "error_message": "=join(' | ',@(1,&))"
      }
    }
  }
]

现在我尝试将这两者结合起来,如果成功为false,则显示错误消息,或者返回要保存的模型。我尝试将成功放入我的转换中,但它总是返回null。在这方面,最佳实践是什么...

rjee0c15

rjee0c151#

可以使用此等级库:
我只是在代码中添加了第一个shift操作符,并将data键更改为truefalse,因此最终代码如下所示:

[
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "data": "@(1,success)"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "message": "body.info_&",
      "true": {
        "#1": "body.good_id",
        "series": "body.policy_&",
        "number": "body.policy_&",
        "end_date": "body.policy_&",
        "direct_settlement_cover": {
          "1": {
            "#Decontare directa": "clauses[0].clause_name",
            "#1": "clauses[0].clause_id"
          }
        },
        "direct_settlement": "clauses[0].premium",
        "installments": {
          "*": {
            "*": "body.&2[&1].&",
            "value": "body.&2[&1].amount"
          }
        }
      },
      "false": {
        "*": {
          "*": "body.error_message"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "good_id": "=toInteger",
        "error_message": "=join(' | ',@(1,&))"
      }
    }
  }
]

相关问题