Search code examples
etlapache-nifiminifyjolt

Unable to transform below json using jolt transformation


I have a JSON which have studentId's who play cricket and I want a student list for each object of the group array. But the output is getting merged into the same student list. I tried iterating each and every studentId and getting the below output. And I wish to get the output like in the below expected/desired format. Any help?

Input:

{
  "studentEligibility": {
    "sportsEligibility": {
      "cricketEligibility": [
        {
          "group": [
            {
              "multiPlayGame": [
                {
                  "studentId": "2aefcb01-fe81-4760-b531-9767c2e6d322_209537"
                },
                {
                  "studentId": "2aefcb01-fe81-4760-b531-9767c2e6d322_217649"
                }
              ]
            }
          ]
        },
        {
          "group": [
            {
              "multiPlayGame": [
                {
                  "studentId": "2aefcb01-fe81-4760-b531-9767c2e6d322_217609"
                }
              ]
            },
            {
              "multiPlayGame": [
                {
                  "studentId": "2aefcb01-fe81-4760-b531-9767c2e6d322_216386"
                }
              ]
            }
          ]
        },
        {
          "group": [
            {
              "multiPlayGame": [
                {
                  "studentId": "2aefcb01-fe81-4760-b531-9767c2e6d322_217008"
                },
                {
                  "studentId": "2aefcb01-fe81-4760-b531-9767c2e6d322_217628"
                }
              ]
            }
          ]
        }
      ]
    }
  }
}

Jolt Spec:

[
  {
    "operation": "shift",
    "spec": {
      "studentEligibility": {
        "sportsEligibility": {
          "cricketEligibility": {
            "*": {
              "group": {
                "*": {
                  "multiPlayGame": {
                    "*": {
                      "studentId": "team[&5].players[]"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

Current Ouptut:

{
  "team": [
    {
      "players": [
        "2aefcb01-fe81-4760-b531-9767c2e6d322_209537",
        "2aefcb01-fe81-4760-b531-9767c2e6d322_217649"
      ]
    },
    {
      "players": [
        "2aefcb01-fe81-4760-b531-9767c2e6d322_217609",
        "2aefcb01-fe81-4760-b531-9767c2e6d322_216386"
      ]
    },
    {
      "players": [
        "2aefcb01-fe81-4760-b531-9767c2e6d322_217008",
        "2aefcb01-fe81-4760-b531-9767c2e6d322_217628"
      ]
    }
  ]
}

Expected / Desired output:

{
  "team": [
    {
      "players": [
        "2aefcb01-fe81-4760-b531-9767c2e6d322_209537",
        "2aefcb01-fe81-4760-b531-9767c2e6d322_217649"
      ]
    },
    {
      "players": [
        "2aefcb01-fe81-4760-b531-9767c2e6d322_217609"
      ]
    },
    {
      "players": [
        "2aefcb01-fe81-4760-b531-9767c2e6d322_216386"
      ]
    },
    {
      "players": [
        "2aefcb01-fe81-4760-b531-9767c2e6d322_217008",
        "2aefcb01-fe81-4760-b531-9767c2e6d322_217628"
      ]
    }
  ]
}

Solution

  • Jolt Spec Solution

    [
      {
        "operation": "shift",
        "spec": {
          "studentEligibility": {
            "sportsEligibility": {
              "cricketEligibility": {
                "*": {
                  "group": {
                    "*": "teams[].players[]"
                  }
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "teams": {
            "*": {
              "players": {
                "*": {
                  "multiPlayGame": "team.players[]"
                }
              }
            }
          }
        }
      },
    
      {
        "operation": "shift",
        "spec": {
          "team": {
            "players": {
              "*": {
                "*": {
                  "studentId": "team[&2].players[]"
                }
              }
            }
          }
        }
      }
    ]