Search code examples
arraysjsonjolt

Adding a key-value pair to every json element using JOLT v.0.1.1


I have this JSON input:

{
  "entityType": "job",
  "id": 908815,
  "properties": {
    "AvatureID": 908815,
    "TemplateName": [
      "jicofo package",
      "jitsi meet fork",
      "jitsi-videobridge2 package"
    ],
    "CaseType": [
      "Internal project",
      "Internal project",
      "Internal project"
    ],
    "ProjectType": [
      "Cloud Services",
      "Development",
      "Cloud Services"
    ],
    "Team": [
      "Cloud Operations",
      "Video",
      "Cloud Operations"
    ],
    "Category": [
      "Routine Ops",
      "Platform enhancement",
      "Routine Ops"
    ],
    "WorkflowStepID": [
      "579",
      "531",
      "579"
    ]
  }
}

And I need to get this output using a JOLT (v.0.1.1) transformation:

[
  {
    "ParentCaseID": 908815,
    "TemplateName": "jicofo package",
    "CaseType": "Internal project",
    "ProjectType": "Cloud Services",
    "Team": "Cloud Operations",
    "Category": "Routine Ops",
    "WorkflowStepID": "579"
  },
  {
    "ParentCaseID": 908815,
    "TemplateName": "jitsi meet fork",
    "CaseType": "Internal project",
    "ProjectType": "Development",
    "Team": "Video",
    "Category": "Platform enhancement",
    "WorkflowStepID": "531"
  },
  {
    "ParentCaseID": 908815,
    "TemplateName": "jitsi-videobridge2 package",
    "CaseType": "Internal project",
    "ProjectType": "Cloud Services",
    "Team": "Cloud Operations",
    "Category": "Routine Ops",
    "WorkflowStepID": "579"
  }
]

But I am having some issues when I try to add the ParentCaseID.

I am currently using this JOLT:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "TemplateName": {
          "*": {
            "@": "[&1].TemplateName"
          }
        },
        "CaseType": {
          "*": {
            "@": "[&1].CaseType"
          }
        },
        "ProjectType": {
          "*": {
            "@": "[&1].ProjectType"
          }
        },
        "Team": {
          "*": {
            "@": "[&1].Team"
          }
        },
        "Category": {
          "*": {
            "@": "[&1].Category"
          }
        },
        "WorkflowStepID": {
          "*": {
            "@": "[&1].WorkflowStepID"
          }
        }
      }
    }
  }
]

And its output is the next one:

[
  {
    "TemplateName": "jicofo package",
    "CaseType": "Internal project",
    "ProjectType": "Cloud Services",
    "Team": "Cloud Operations",
    "Category": "Routine Ops",
    "WorkflowStepID": "579"
  },
  {
    "TemplateName": "jitsi meet fork",
    "CaseType": "Internal project",
    "ProjectType": "Development",
    "Team": "Video",
    "Category": "Platform enhancement",
    "WorkflowStepID": "531"
  },
  {
    "TemplateName": "jitsi-videobridge2 package",
    "CaseType": "Internal project",
    "ProjectType": "Cloud Services",
    "Team": "Cloud Operations",
    "Category": "Routine Ops",
    "WorkflowStepID": "579"
  }
]

Is there any modification I can make to that JOLT so I can achieve the results I am looking for?

Thank you in advance!


Solution

  • What is the problem when adding the ParentCaseID? I tried the following and it worked:

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "TemplateName": {
              "*": {
                "@(3,id)": "[&1].ParentCaseID",
                "@": "[&1].TemplateName"
              }
            },
            "CaseType": {
              "*": {
                "@": "[&1].CaseType"
              }
            },
            "ProjectType": {
              "*": {
                "@": "[&1].ProjectType"
              }
            },
            "Team": {
              "*": {
                "@": "[&1].Team"
              }
            },
            "Category": {
              "*": {
                "@": "[&1].Category"
              }
            },
            "WorkflowStepID": {
              "*": {
                "@": "[&1].WorkflowStepID"
              }
            }
          }
        }
      }
    ]
    

    Hope that works for you.