简体   繁体   中英

JSON to nested JSON nested array using Jolt Transformation

How can I transform a flat JSON to the nested JSON using jolt transformation? I am newbie to JSON and jolt.

Input:

[
  {
    "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": ""
  }
]

Expected Output:

[
  {
    "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": ""
      }
    ]
  }
]

Edit : I want add one new field in SubFunds , but new field not present in JSON file it is calculated field, Can I add Example As-is

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

and rename fields :

"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"

Indeed, you can bring the nested arrays with a shift transformation where pay attention to grouping by id ( @(1,Id) ) and the breakdowns for sub-arrays( @(1,Id).funds[&1].subFunds[&1] ), and sub-objects( @(1,Id).funds[&1].balance ), and then use extra transformations in order to make the JSON smooth to the desired design such as

[
  {
    "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": ""
              }
            }
          }
        }
      }
    }
  }
 ]

where derived attributes Total and SubTotal are added along with renamed attribute names as lately added to the question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM