0
votes

I'm trying to write a spec to do the below transformation using jolt transformation. I'm only interested in changing the name of key in json, value should remain same. Help me out.

Input Json:

[
  {
    "list1": [
      {
        "id": "CPP1600000009846",
        "list2": [
          {
            "amount": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "code": "CONAMP"
          },
          {
            "amount": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "code": "PCCPRI"
          },
          {
            "amount": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "code": "PCCPCI"
          },
          {
            "amount": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "code": "PCCPII"
          }
        ]
      },
      {
        "id": "CPP1600000009846",
        "list2": [
          {
            "amount": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "code": "CONEIT"
          },
          {
            "amount": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "code": "CONCRT"
          },
          {
            "amount": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "code": "CONNCT"
          }
        ]
      }
    ]
  }
]

Expected output:

[
  {
    "listA": [
      {
        "Num": "CPP1600000009846",
        "listB": [
          {
            "rate": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "covg_code": "CONAMP"
          },
          {
            "rate": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "covg_code": "PCCPRI"
          },
          {
            "rate": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "covg_code": "PCCPCI"
          },
          {
            "rate": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "covg_code": "PCCPII"
          }
        ]
      },
      {
        "Num": "CPP1600000009846",
        "listB": [
          {
            "rate": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "covg_code": "CONEIT"
          },
          {
            "rate": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "covg_code": "CONCRT"
          },
          {
            "rate": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "covg_code": "CONNCT"
          }
        ]
      }
    ]
  }
]
2

2 Answers

1
votes

Sorry for the late replay, Please use the below spec

[
    { 
        "operation": "shift", 
        "spec": { 
            "list1": { 
                "*": {    
                    "id": "listA[&1].Num",    
                    "list2": {    
                        "*": {    
                            "code": "listA[&3].listB[&1].covg_code",    
                            "amount": {    
                                "formattedPrimeAmount": "listA[&4].listB[&2].rate.formattedPrimeAmount",    
                                "primeAmount": "listA[&4].listB[&2].rate.primeAmount"    
                             }    
                         }    
                     }    
                 }    
             }    
         }    
     }    
]    
0
votes

use following spec

[
  {
    "operation": "shift",
    "spec": {
        "list1" : {
            "*": {
                "id" : "listA[&1].Num",
                "list2" : {
                    "*": {
                        "amount" : "listA[&1].listB[&3].rate",
                        "code" : "listA[&1].listB[&3].covg_code"
                    }
                }
            }
        }
    }
  }

]

run this spec using following code

    List<Object> chainrSpecJSON = JsonUtils.filepathToList( "D:\\path\\to\\spec.json" );
    Chainr chainr = Chainr.fromSpec( chainrSpecJSON );

    List<Object> inputJSONList = JsonUtils.filepathToList( "D:\\path\\to\\input.json" );

    List<Object> outputList = new ArrayList<Object>();

    for(Object singleObj : inputJSONList){
        Object transformedOutput = chainr.transform( singleObj );
        outputList.add(transformedOutput);
    }