Arun Sai
Arun Sai

Reputation: 1972

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"
      ]
    }
  ]
}

Upvotes: 1

Views: 74

Answers (1)

ggordon
ggordon

Reputation: 10035

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[]"
            }
          }
        }
      }
    }
  }
]


Upvotes: 2

Related Questions