driven_spider
driven_spider

Reputation: 495

use jolt transform and convert flat json to complicated nested json array

I have a question on transforming flat json to nested json using jolt. And I'm very new to jolt, This is my input

{
 "id": "LIKKI MOSORU",
 "aff_id": "WOOD",
 "aff_name": "WOOD-LOVE",
 "aff_desc": "WOOD INC.",
 "aff_corrltn_name": "MARVEL MEMBERSHIP",         
 "aff_corrltn_id": "8999938",
 "affil_trans_id": "222222",
 "misc_aff_dtl_name": "MBR-FIRST-NAME",
 "misc_aff_dtl_value" :"KKKKKLYYYY",
 "oper_id": "AP",
 "create_timestamp":"2018-10-16-04:00",
 "update_timestamp": "2018-10-17-04:00",
 "active_rcrd": "Y"
 }

I wrote jolt spec and I'm not getting the desired output

    [
      {
        "operation": "shift",
        "spec": {
          "misc_aff_dtl_*": "misc_aff_dtl.&(0,1)",
          "aff_corrltn_*": "aff_corrltn.&(0,0)",
          "aff_*": "aff.&(0,0)",
          "rewards_id": "rewards_id",
          "oper_id": "oper_id",
          "create_timestamp": "create_timestamp",
          "update_timestamp": "update_timestamp",
          "active_rcrd": "active_rcrd"
        }
    }
    ]

My expected output is :

    {
    "rewards_id": "Jeannine Rosario",
    "aff": [
        {        
    "aff_id": "WOOD",
    "aff_name": "WOOD-LOVE",
    "aff_desc": "WOOD INC.",
    "aff_corrltn": [
    {        
        "aff_corrltn_name": "MARVEL MEMBERSHIP",         
        "aff_corrltn_id": "8999938",
        "affil_trans_id": "222222",
        "misc_aff_dtl": [        
            {  
                "name":"MBR-FIRST-NAME",
                "value":"DANJALKSA"
            }
          ] 
    }           
    ],
     "oper_id": "AP",
     "create_timestamp":"2018-10-16-04:00",
     "update_timestamp": "2018-10-17-04:00",
     "active_rcrd": "Y"
     }
     ]
    }

Can anyone who is a jolt expert, help me get the desired output. Should I use multiple transformations in jolt or is it possible to get th edesired output in one jolt transformer?

Upvotes: 1

Views: 703

Answers (1)

Matthew Warman
Matthew Warman

Reputation: 3442

Adding arrays using [0] will have the desired effect:

[
  {
    "operation": "shift",
    "spec": {
      "rewards_id": "rewards_id",
      "aff_*": "aff.[0].&(0,0)",
      "misc_aff_dtl_*": "aff.[0].aff_corrltn.[0].misc_aff_dtl.[0].&(0,1)",
      "aff_corrltn_*": "aff.[0].aff_corrltn.[0].&(0,0)",
      "affil_trans_id": "aff.[0].aff_corrltn.[0].&",
      "oper_id": "aff.[0].oper_id",
      "create_timestamp": "aff.[0].create_timestamp",
      "update_timestamp": "aff.[0].update_timestamp",
      "active_rcrd": "aff.[0].active_rcrd"
    }
  }
]

Upvotes: 1

Related Questions