Home > front end >  Jolt: How to level-up elements from an object?
Jolt: How to level-up elements from an object?

Time:01-24

I have the following JSON :

{
  "Content": [
    {
      "CandidateFirstName": "Nagu",
      "Applications": {
        "ApplicationId": "456",
        "Sarasa": "test"
      }
    },
    {
      "CandidateFirstName": "Deleted",
      "Applications": {
        "ApplicationId": "123",
        "Sarasa": "test2"
      }
    }
  ]
}

and I'd like to have the following output:

[
  {
    "FirstName": "Nagu",
    "ApplicationsId": "456",
    "Sarasa": "test"
  },
  {
    "FirstName": "Deleted",
    "ApplicationsId": "123",
    "Sarasa": "test2"
  }
]

I'm close with the following jolt, but I'm not being able to remove the object "Applications":

[
  {
    "operation": "shift",
    "spec": {
      "Content": {
        "*": {
          "CandidateFirstName": "[&1].FirstName",
          "Applications": "[&1].Applications"
        }
      }
    }
  }
]

Do you know how to achieve this?

Thanks in advance!

CodePudding user response:

You can use this short and concise spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "Candidate*": "[&1].&(0,1)",
          "*": {
            "*": "[&2].&"
          }
        }
      }
    }
  }
]

CodePudding user response:

Yes, you were quite close. You have to just add one more level of nested section for Applications section

[
 {
 "operation": "shift",
 "spec": {
  "Content": {
    "*": {
      "CandidateFirstName": "[&1].FirstName",
      "Applications": {
         "*": "[&2].&"
      }
    }
   }
  }
 }
]

CodePudding user response:

You can use two consecutive shift transformation specs

[
  {//get individual objects nested within square brackets
    "operation": "shift",
    "spec": {
      "Content": {
        "*": {
          "Applications": {
            "@1,CandidateFirstName": "&2[#1].FirstName",
            "*": "&2[#2].&"
          }
        }
      }
    }
  },
  {// get rid of the keys of index values
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

or use a single spec such as

[
  {
    "operation": "shift",
    "spec": {
      "Content": {
        "*": {
          "Candidate*": "[#2].FirstName",// the expression "&(0,1)" replicates the literal extracted from the current( 0th ) level of the 1st asterisk(might be multiple) which is stated on the LHS 
          "@Applications.ApplicationId": "[#2].ApplicationId",
          "@Applications.Sarasa": "[#2].Sarasa"
        }
      }
    }
  }
]

where [#2], as having a # wildcard and nested within square brackets and staying on the right-hand-side, represents traversing one colon(:), and one opening curly brace { those makes 2 in order to reach the level of the indexes of the Content array to grab them.

Yet, there's another method as follows :

[
  {
    "operation": "shift",
    "spec": {
      "Content": {
        "*": {
          "Appl*": {// represents the key of the node starts with "Appl" in order to shorten the literal "Applications"
            "@1,CandidateFirstName": "[&2].FirstName",// need to go one level up to get the value of the "CandidateFirstName" attribute
            "*": "[&2].&"
          }
        }
      }
    }
  }
]

where [&2] on the RHS represents traversing two opening curly braces in contrast to the [#2]

  • Related