Search code examples
jsonapache-nifijolt

Duplicating a value to every JSON element using JOLT v0.1.1


I have this JSON input:

{
  "entityType": "job",
  "id": 908848,
  "properties": {
    "TemplateName": [
      "not internal case",
      "not internal case"
    ],
    "CaseType": [
      "Technical Services",
      "Technical Services"
    ],
    "ProjectType": [
      "Integrations",
      "Integrations"
    ],
    "Team": [
      "Integrations Services",
      "Integrations Services"
    ],
    "Category": [
      "Project",
      "Project"
    ],
    "WorkflowStepID": [
      "942",
      "942"
    ],
    "ParentCaseID": 908848,
    "CaseName": [
      "Test Case #1000000",
      "Test #2"
    ],
    "CaseWorkflow": [
      "Integrations Project",
      "Integrations Project"
    ],
    "Username": [
      "Gaswutuq Kamroku, Oyupobu",
      "Gaswutuq Kamroku, Oyupobu"
    ],
    "CaseDescription": [
      "This is a test for the Assignee field",
      ""
    ],
    "CustomerAccount": "132076 - Zeslely Pishigc",
    "CustomerInstance": "sandboxkellogg",
    "ParentTitle": "External Parent Case",
    "Tag": [
      "Fideo",
      ""
    ],
    "AssignedTo": [
      "Xioc, Waxuuli",
      ""
    ],
    "TemplateDescription": [
      "Integrations services default description",
      "Integrations services default description"
    ],
    "Environment": [
      {
        "EnvironmentType": "Sandbox",
        "Instance": "sandboxkellogg"
      },
      {
        "EnvironmentType": "Production",
        "Instance": "kellogg"
      }
    ]
  }
}

And I need to get this output using a JOLT transformation:

[ {
  "ParentCaseID" : 908848,
  "TemplateName" : "not internal case",
  "CaseType" : "Technical Services",
  "ProjectType" : "Integrations",
  "Team" : "Integrations Services",
  "Category" : "Project",
  "WorkflowStepID" : "942",
  "CaseName" : "Test Case #1000000",
  "CaseWorkflow" : "Integrations Project",
  "CaseDescription" : "This is a test for the Assignee field",
  "Tag" : "Fideo",
  "AssignedTo" : "Xioc, Waxuuli",
  "TemplateDescription" : "Integrations services default description",
  "Environment" : ["Sandbox", "Production"],
  "Instance" : ["sandboxkellogg", "kellogg"],
  "Username" : "Gaswutuq Kamroku, Oyupobu",
  "CustomerAccount" : "132076 - Zeslely Pishigc",
  "CustomerInstance" : "sandboxkellogg",
  "ParentTitle" : "External Parent Case"
}, {
  "ParentCaseID" : 908848,
  "TemplateName" : "not internal case",
  "CaseType" : "Technical Services",
  "ProjectType" : "Integrations",
  "Team" : "Integrations Services",
  "Category" : "Project",
  "WorkflowStepID" : "942",
  "CaseName" : "Test #2",
  "CaseWorkflow" : "Integrations Project",
  "CaseDescription" : "",
  "Tag" : "",
  "AssignedTo" : "",
  "TemplateDescription" : "Integrations services default description",
  "Environment" : ["Sandbox", "Production"],
  "Instance" : ["sandboxkellogg", "kellogg"]
  "Username" : "Gaswutuq Kamroku, Oyupobu",
  "CustomerAccount" : "132076 - Zeslely Pishigc",
  "CustomerInstance" : "sandboxkellogg",
  "ParentTitle" : "External Parent Case"
} ]

I am having issues to replicate both EnvironmentType and Instance in the separate JSONs. In my output I can receive one or more "EnvironmentType" and "Instance" objects within the "Environment" array. I am needing that every value is transformed and placed in the correspondent variables for each JSON in the output.

This is the JOLT that I'm currently using:

[
  {
    "operation": "shift",
    "spec": {
      "properties": {
        "TemplateName": {
          "*": {
            "@(3,id)": "[&1].ParentCaseID",
            "@": "[&1].TemplateName"
          }
        },
        "CaseType": {
          "*": {
            "@": "[&1].CaseType"
          }
        },
        "ProjectType": {
          "*": {
            "@": "[&1].ProjectType"
          }
        },
        "Team": {
          "*": {
            "@": "[&1].Team"
          }
        },
        "Category": {
          "*": {
            "@": "[&1].Category"
          }
        },
        "WorkflowStepID": {
          "*": {
            "@": "[&1].WorkflowStepID"
          }
        },
        "CaseName": {
          "*": {
            "@": "[&1].CaseName"
          }
        },
        "CaseWorkflow": {
          "*": {
            "@": "[&1].CaseWorkflow"
          }
        },
        "CaseDescription": {
          "*": {
            "@": "[&1].CaseDescription"
          }
        },
        "Tag": {
          "*": {
            "@": "[&1].Tag"
          }
        },
        "AssignedTo": {
          "*": {
            "@": "[&1].AssignedTo"
          }
        },
        "TemplateDescription": {
          "*": {
            "@": "[&1].TemplateDescription"
          }
        },
        "Environment": {
          "*": {
            "@(EnvironmentType)": "[&].Environment.@(EnvironmentType)",
            "@(Instance)": "[&].Instance.@(Instance)"
          }
        },
        "Username": {
          "*": {
            "@": "[&1].Username",
            "@(2,CustomerAccount)": "[&1].CustomerAccount",
            "@(2,CustomerInstance)": "[&1].CustomerInstance",
            "@(2,ParentTitle)": "[&1].ParentTitle"
          }
        }
      }
    }
  }
]

And the output is this:

[ {
  "ParentCaseID" : 908848,
  "TemplateName" : "not internal case",
  "CaseType" : "Technical Services",
  "ProjectType" : "Integrations",
  "Team" : "Integrations Services",
  "Category" : "Project",
  "WorkflowStepID" : "942",
  "CaseName" : "Test Case #1000000",
  "CaseWorkflow" : "Integrations Project",
  "CaseDescription" : "This is a test for the Assignee field",
  "Tag" : "Fideo",
  "AssignedTo" : "Xioc, Waxuuli",
  "TemplateDescription" : "Integrations services default description",
  "Environment" : {
    "Sandbox" : "Sandbox"
  },
  "Instance" : {
    "sandboxkellogg" : "sandboxkellogg"
  },
  "Username" : "Gaswutuq Kamroku, Oyupobu",
  "CustomerAccount" : "132076 - Zeslely Pishigc",
  "CustomerInstance" : "sandboxkellogg",
  "ParentTitle" : "External Parent Case"
}, {
  "ParentCaseID" : 908848,
  "TemplateName" : "not internal case",
  "CaseType" : "Technical Services",
  "ProjectType" : "Integrations",
  "Team" : "Integrations Services",
  "Category" : "Project",
  "WorkflowStepID" : "942",
  "CaseName" : "Test #2",
  "CaseWorkflow" : "Integrations Project",
  "CaseDescription" : "",
  "Tag" : "",
  "AssignedTo" : "",
  "TemplateDescription" : "Integrations services default description",
  "Environment" : {
    "Production" : "Production"
  },
  "Instance" : {
    "kellogg" : "kellogg"
  },
  "Username" : "Gaswutuq Kamroku, Oyupobu",
  "CustomerAccount" : "132076 - Zeslely Pishigc",
  "CustomerInstance" : "sandboxkellogg",
  "ParentTitle" : "External Parent Case"
} ]

I can't understand what is the modification I need to make for the JOLT to transform the data to my desired results, does anybody has a clue?

Thanks in advance!


Solution

  • You can use the following transformation :

    [
      {
        "operation": "shift",
        "spec": {
          "properties": { // prepare attributes
            "TemplateName|CaseType|ProjectType|Team|Category|WorkflowStepID|CaseName|CaseWorkflow|CaseDescription|Tag|AssignedTo|TemplateDescription|Username": {
              "*": {
                "@3,id": "main[&1].ParentCaseID",
                "@": "main[&1].&2",
                "@2,CustomerAccount": "main[&1].CustomerAccount",
                "@2,CustomerInstance": "main[&1].CustomerInstance",
                "@2,ParentTitle": "main[&1].ParentTitle"
              }
            },
            "Environment": { // prepare array elements
              "*": {
                "*": {
                  "@": "arrays.&1"
                }
              }
            }
          }
        }
      },
      { // in order to get non-arrays within the main array
        "operation": "cardinality",
        "spec": {
          "main": {
            "*": {
              "*": "ONE"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "main": {
            "*": {
              "@2,arrays": { "*": "[&1].&" }, // go two levels up the tree to grab the values from the "main" array
              "*": "[&1].&"
            }
          }
        }
      }
    ]