skip to Main Content

I am new to jolt transformation. what would be the jolt spec to transform my source json

{
  "version": "123",
  "payload": {
    "details": {
      "payload_json": "{"FName":"rishi","LName":"dx","Id":"ABC123","aadharId":"XYZ22"}"
    }
  }
}

to target JSON

{
  "firstName": "rishi",
  "lastName": "dx",
  "id": "ABC123",
  "officialIds": [
    {
      "type": "aadhar",
      "value": "XYZ22"
    }
  ]
}

I tried using Google Gemini and Micosoft Copilot to get the jolt spec, but neither of them are generating correct spec.

2

Answers


  1. To achieve your desired transformation using Jolt, you can use a combination of shift and modify transformation steps. Here’s the Jolt spec to transform your source JSON to the target JSON:

    [
    {
    "operation": "shift",
    "spec": {
    "payload": {
    "details": {
    "payload_json": {
    "*": {
    // Move all fields from payload_json to the top level
    "@": "&"
    }
    }
    }
    }
    }
    },
    {
    "operation": "shift",
    "spec": {
    // Rename fields according to the target structure
    "FName": "firstName",
    "LName": "lastName",
    "Id": "id",
    "aadharId": "officialIds[0].value"
    }
    },
    {
    "operation": "modify-default-beta",
    "spec": {
    // Add type field to officialIds
    "officialIds[0].type": "aadhar"
    }
    }
    ]

    This Jolt spec will perform the following steps:

    Shift Operation 1:
    Extract the nested JSON from payload_json and move its contents to the top level.

    Shift Operation 2:
    Rename the fields to match the target JSON structure.
    Modify Operation: Add the type field to the officialIds array.

    Applying this Jolt spec to your source JSON will produce the desired target JSON. You can test this transformation using a Jolt transformation tool or library in your preferred programming language.

    Login or Signup to reply.
  2. You can solve this easily using JSLT:

    let json_payload = from-json(.payload.details.payload_json)
        
    {
      "firstName": $json_payload.FName ,
      "lastName":  $json_payload.LName ,
      "id":  $json_payload.Id,
      "officialIds":[for ($json_payload) {"type":.key,"value":.value } if(not(contains(.key,["FName","LName","Id"])))]  
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search