我们如何在JSONata中的对象中替换和更改数组?

djmepvbi  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(105)

我有一个JSON对象,其中我有一个键policyList,其值类型为数组。在policyList中,名为product的键下还有另一个数组。
这里我想改变amount的值,它位于数组product的每个对象中。
我可以改变它,但最后我只得到了amount的数组,因为我想要修改amount值的整个原始对象。
请参阅this link的数据在JSONata练习链接,我有数据和表达式,我写的。
JSON:

{
  "admissionDate": "2023-10-04",
  "claimCaseId": 123,
  "claimCaseStatusDescp": "Waiting",
  "claimTypeDescp": "Hospitalization",
  "dischargeDate": "2023-10-20",
  "hospital": "abc",
  "policyList": [
    {
      "policyNo": "P1",
      "product": [
        {
          "amount": 10243,
          "claimDecision": "7",
          "claimDecisionDescp": "Pending",
          "productName": "Auto"
        },
        {
          "amount": 1023,
          "claimDecision": "7",
          "claimDecisionDescp": "Pending",
          "productName": "Mob"
        },
        {
          "amount": 100000,
          "claimDecision": "8",
          "claimDecisionDescp": "Approved",
          "productName": "PA"
        }
      ]
    }
  ],
  "reasonCode": ""
}

字符串
我写的表达式是:

$map($.policyList.product.amount, function($amount) {
    $formatNumber($amount,'#,###')
})


接下来的输出是:

[
  "10,243",
  "1,023",
  "100,000"
]


预期输出为:

{
  "admissionDate": "2023-10-04",
  "claimCaseId": 123,
  "claimCaseStatusDescp": "Waiting",
  "claimTypeDescp": "Hospitalization",
  "dischargeDate": "2023-10-20",
  "hospital": "abc",
  "policyList": [
    {
      "policyNo": "P1",
      "product": [
        {
          "amount": "10,243",
          "claimDecision": "7",
          "claimDecisionDescp": "Pending",
          "productName": "Auto"
        },
        {
          "amount": "1,023",
          "claimDecision": "7",
          "claimDecisionDescp": "Pending",
          "productName": "Mob"
        },
        {
          "amount": "100,000",
          "claimDecision": "8",
          "claimDecisionDescp": "Approved",
          "productName": "PA"
        }
      ]
    }
  ],
  "reasonCode": ""
}

注意事项:在预期的输出中,我将amount显示为字符串,但我可以使用任何数据类型,因为我对值的操作在输出中存在

wgx48brx

wgx48brx1#

要就地更新嵌套对象,可以使用Transform操作符:

$$ ~> |policyList.product|{"amount": $formatNumber(amount,'#,###')}|

字符串
Stedi Playground上查看。

相关问题