JSON 到嵌套 JSON 使用 Jolt 转换的嵌套数组

JSON to nested JSON nested array using Jolt Transformation

如何使用颠簸转换将平面 JSON 转换为嵌套 JSON?我是 JSON 和 jolt.

的新手

输入:

[
  {
    "Id": 1,
    "number": 6,
    "name": "axa",
    "code": "wewe",
    "amount": "100",
    "currency": "doller",
    "othercurrency": "aug",
    "subfund": "axa1",
    "noOfUnits": 0,
    "unitPrice": 0,
    "insurerId": ""
  },
  {
    "Id": 2,
    "number": 6,
    "name": "visa",
    "code": "wewe",
    "amount": "100",
    "currency": "doller",
    "othercurrency": "aug",
    "subfund": "visa1",
    "noOfUnits": 0,
    "unitPrice": 0,
    "insurerId": ""
  },
  {
    "Id": 1,
    "number": 6,
    "name": "master",
    "code": "qqq",
    "amount": "100",
    "currency": "doller",
    "othercurrency": "aug",
    "subfund": "master1",
    "noOfUnits": 0,
    "unitPrice": 0,
    "insurerId": ""
  }
]

预期输出:

[
  {
    "id": "1",
    "number": 6,
    "funds": [
      {
        "name": "axa",
        "code": "wewe",
        "balance": {
          "amount": 100,
          "currency": "doller",
          "othercurrency": "aug"
        },
        "subFunds": [
          {
            "name": "axa1",
            "noOfUnits": 0,
            "unitPrice": 0
          }
        ],
        "insurerId": ""
      },
      {
        "name": "master",
        "code": "qqq",
        "balance": {
          "amount": 100,
          "currency": "doller",
          "othercurrency": "aug"
        },
        "subFunds": [
          {
            "name": "master1",
            "noOfUnits": 0,
            "unitPrice": 0
          }
        ],
        "insurerId": ""
      }
    ]
  },
  {
    "id": "2",
    "number": 6,
    "funds": [
      {
        "name": "visa",
        "code": "wewe",
        "balance": {
          "amount": 100,
          "currency": "doller",
          "othercurrency": "aug"
        },
        "subFunds": [
          {
            "name": "visa1",
            "noOfUnits": 0,
            "unitPrice": 0
          }
        ],
        "insurerId": ""
      }
    ]
  }
]

编辑: 我想在 SubFunds 中添加一个新字段,但 JSON 文件中不存在新字段它是计算字段,我可以按原样添加示例

"subFunds": [ { "name": "axa1", "noOfUnits": 0, "unitPrice": 0 } 
// to be 
"subFunds": [ { "name": "axa1", "noOfUnits": 0, "unitPrice": 0, "Total": unitPrice * noOfUnits, "SubTotal": Total+300 }

并重命名字段:

"Id": "@(1,Id).&"
// to 
"Id": "@(1,Id).k_id"
"amount": "@(1,Id).funds[&1].balance.&"
// to 
"amount": "@(1,Id).funds[&1].k_bal"
"noOfUnits": "@(1,Id).funds[&1].subFunds[&1].&"
// to 
"noOfUnits": "@(1,Id).funds[&1].subFunds[&1].k_units"

事实上,您可以将嵌套数组与 shift 转换一起使用,其中注意按 id(@(1,Id)) 分组和子细分-数组(@(1,Id).funds[&1].subFunds[&1])和子对象(@(1,Id).funds[&1].balance),然后使用额外的转换以使 JSON 平滑到所需的设计,例如

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "Id": "@(1,Id).k_id",
        "number": "@(1,Id).&",
        "name": "@(1,Id).funds[&1].&",
        "code": "@(1,Id).funds[&1].&",
        "amount": "@(1,Id).funds[&1].balance.k_bal",
        "currency": "@(1,Id).funds[&1].balance.&",
        "othercurrency": "@(1,Id).funds[&1].balance.&",
        "@(0,name)": "@(1,Id).funds[&1].subFunds[&1].name",
        "noOfUnits": "@(1,Id).funds[&1].subFunds[&1].k_units",
        "unitPrice": "@(1,Id).funds[&1].subFunds[&1].&",
        "insurerId": "@(1,Id).&"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "k_id": "ONE",
        "number": "ONE",
        "insurerId": "ONE"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  },
  {
    "operation": "sort",
    "spec": {}
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "funds": {
          "*": {
            "subFunds": {
              "*": {
                "nou": "=divide(1,@(1,k_units))",
                "Tot": "=divide(@(1,unitPrice),@(1,nou))",
                "Total": "=toInteger(@(1,Tot))",
                "SubTotal": "=intSum(@(1,Total),300)"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "*": {
                "nou": "",
                "Tot": ""
              }
            }
          }
        }
      }
    }
  }
 ]

其中添加派生属性 TotalSubTotal 以及最近添加到问题中的重命名属性名称。