0

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 Answers2

1

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"    
                             }    
                         }    
                     }    
                 }    
             }    
         }    
     }    
]    
ash
  • 1,224
  • 3
  • 26
  • 46
venucb
  • 21
  • 2
0

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);
    }
Arjit
  • 421
  • 7
  • 20
  • updated answer to match exact use case. It will certainly give you desired output now. Let me know if some explanation needed. Read wildcards from jolt documentation for better understanding. – Arjit Jun 02 '16 at 08:15
  • I'm using this http://jolt-demo.appspot.com/#inception to write the spec. Here it is giving null. – Sandeep Kumar M Jun 06 '16 at 12:39
  • With your code i got output, but it is not expected output. Your spec has created 4 objects under list A which is suppose to be only 2. Spec is only suppose to change the name of objects and fields but it is changing both name and their positions. i.e It is taking first objects from all different list2s, and putting them as first object of listA. similarly all 2nd to 2nd, 3rd to 3rd so on. Thank you for your time. It was really helpful. – Sandeep Kumar M Jun 06 '16 at 13:30