skip to Main Content

For the details mentioned below, I would like to move the email.values* to payload.*.content.values (preserving the changes)

I have created below Jolt –

[
  {
    "operation": "shift",
    "spec": {
      // set values of the object keys to their accountIds
      // while taking out the value of request.body.inputAccountId
      "*": {
        "request": {
          "body": {
            "inputId": "inputId",
            "service": "sName"
          }
        },
        "*": {
          "@": "@2,accountId.&"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@2,&": {
          "familyRole": {
            "//": "Comment: Based on Family role get the details, familyRole1=Organizer, familyRole2=Guardian, familyRole3=child",
            "1|2": {
              "@(2,accountId)": "payload.@(3,accountId).accountId",
              "@(2,locale)": "payload.@(3,accountId).content.locale",
              "@(2,onlineId)": "payload.@(3,accountId).content.values.onlineId",
              "@(2,emailAddresses)": {
                "*": {
                  "isMain": {
                    "true": {
                      "@(2,address)": "payload.@(7,accountId).destination.emailAddress"
                    }
                  }
                }
              },
              "@(5,sName)": "payload.@(3,accountId).content.values.sName"
            }
          }
        }
      },
      "inputId": {
        "*": {
          "@2,&": {
            "onlineId": "..payload.childId"
          }
        }
      }
    }
  }
]

For given input –

[
  {
    "request": {
      "body": {
        "inputId": "2211",
        "service": "trial"
      }
    }
  },
  {
    "accountId": "1234",
    "role": 1,
    "gender": "f",
    "signinId": "[email protected]",
    "onlineId": "one"
  },
  {
    "accountId": "1122",
    "role": 2,
    "gender": "f",
    "signinId": "[email protected]",
    "onlineId": "two"
  },
  {
    "accountId": "2211",
    "role": 3,
    "gender": "f",
    "signinId": "[email protected]",
    "onlineId": "three"
  }
]

It gives me below output –

{
  "payload" : {
    "childId" : "three",
    "1234" : {
      "accountId" : "1234",
      "content" : {
        "values" : {
          "onlineId" : "one",
          "sName" : "trial"
        }
      }
    },
    "1122" : {
      "accountId" : "1122",
      "content" : {
        "values" : {
          "onlineId" : "two",
          "sName" : "trial"
        }
      }
    }
  }
}


Expected output:

{
  "payload" : {
    "1234" : {
      "accountId" : "1234",
      "content" : {
        "values" : {
          "onlineId" : "one",
          "cOnlineId" : "three",
          "sName" : "trial",
        }
      }
    },
    "1122" : {
      "accountId" : "1122",
      "content" : {
        "values" : {
          "onlineId" : "two",
          "cOnlineId" : "three",
          "sName" : "trial"
        }
      }
    }
  }
}

Now I just need help to figure out how can I add that childId in payload.*.content.values ?

I have applied multiple combinations in JOLT Spec but it didn’t works, Please suggest. I also referred this -https://stackoverflow.com/questions/76474146/how-to-insert-json-object-in-nested-json-using-jolt-spec

2

Answers


  1. Chosen as BEST ANSWER

    I was finally able to solve this.

    Earlier, I was trying to update the existing payload and then traverse the tree to move the childId which was coming in the later operation. However, now -

    • first I added the childId while preserving the existing structure
    • Then added that childId using simple "payload.@(3,accountId).content.values.childId" for each account -

    Below is my updated JOLT-

    [
      {
        "operation": "shift",
        "spec": {
          // set values of the object keys to their accountIds
          // while taking out the value of request.body.inputAccountId
          "*": {
            "request": {
              "body": {
                "inputId": "inputId",
                "service": "sName"
              }
            },
            "*": {
              "@": "@2,accountId.&"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          // Preserve the existing structure
          "*": {
            "@": "&",
            "*": "[#2].&"
          },
          // Add onlineId: childId pair
          "inputId": {
            "*": {
              "@2,&": {
                "onlineId": "childId"
              },
              "*": "[#2].&"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "@2,&": {
              "familyRole": {
                "//": "Comment: Based on Family role get the details, familyRole1=Organizer, familyRole2=Guardian, familyRole3=child",
                "1|2": {
                  "@(2,accountId)": "payload.@(3,accountId).accountId",
                  "@(2,locale)": "payload.@(3,accountId).content.locale",
                  "@(2,onlineId)": "payload.@(3,accountId).content.values.onlineId",
                  "@(2,emailAddresses)": {
                    "*": {
                      "isMain": {
                        "true": {
                          "@(2,address)": "payload.@(7,accountId).destination.emailAddress"
                        }
                      }
                    }
                  },
                  "@(5,sName)": "payload.@(3,accountId).content.values.sName",
                  "@(5,childId)": "payload.@(3,accountId).content.values.childId"
                }
              }
            }
          }
        }
      }
    ]
    

  2. You can use the following transformation specs as an alternative option :

    [
      { //make "accountId" values keys of objects while getting
        //unnested attributes from the "request" object 
        "operation": "shift",
        "spec": {
          "*": {
            "request": {
              "*": {
                "*": "&2_&"
              }
            },
            "*": "@1,accountId.&"
          }
        }
      },
      { //get double nested objects to prepare for the upcoming spec
        //in which matching of inner vs. outer keys are handled
        "operation": "shift",
        "spec": {
          "request_se*": "request.sName",
          "*": {
            "@": "@2,request_inputId.&1"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "request": {
            "*": "&1.&"
          },
          "*": {
            "&": { // matching ones
              "on*": "request.cOn&(0,1)"
            },
            "*": { // non-matching ones
              "*": "payload.@1,accountId.&"
            }
          }
        }
      },
      { //rearrange the stuff
        "operation": "shift",
        "spec": {
          "payload": {
            "*": {
              "accountId": "&2.&1.&",
              "onlineId": "&2.&1.values.&",
              "@2,request": { "*": "&3.&1.values.&" }//import from 2 levels outer layer
            }
          }
        }
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search