Search code examples
jsonjolt

In JOLT Spec how to move values from one JSON object to the other nested JSON Object


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": "aa@aa.com",
    "onlineId": "one"
  },
  {
    "accountId": "1122",
    "role": 2,
    "gender": "f",
    "signinId": "bb@aa.com",
    "onlineId": "two"
  },
  {
    "accountId": "2211",
    "role": 3,
    "gender": "f",
    "signinId": "cc@aa.com",
    "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


Solution

  • 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"
                }
              }
            }
          }
        }
      }
    ]