Need help to figure out how to fix a jolt spec

Viewed 39

I am trying to write a specification using Jolt, I have been struggling with it a little, my desired output is:

{
  "Team": [
    {
      "ID": "1",
      "Source": {
        "HV": 1,
        "ID": 1
      }
    },
    {
      "ID": "3",
      "Source": {
        "HV": 2,
        "ID": 3
      }
    }
  ]
}

I have the following input:


[
  {
    "Page": 910407,
    "TimingID": 10014,
    "ScoreInfo": {
      "Inn": 9,
      "TB": 1,
      "Team": [
        {
          "HV": 1,
          "ID": 1,
          "Score": 4
        },
        {
          "HV": 2,
          "ID": 3,
          "Score": 2
        }
      ],
      "HV": null
    }
  }
]

But after execute this specification:

[
  {
    "operation": "shift",
    "spec": {
      "0": {
        "ScoreInfo": {
          "Team": {
            "*": "&"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "Team.@ID.Source"
    }
  }
]

I ended up with this result:

{
  "Team" : {
    "1" : {
      "Source" : {
        "HV" : 1,
        "ID" : 1,
        "Score" : 4
      }
    },
    "3" : {
      "Source" : {
        "HV" : 2,
        "ID" : 3,
        "Score" : 2
      }
    }
  }
}

I would some help to figure out how to solve, I have being reading and practing but still having some doubts in cases, where it needs a little bit more miles using Jolt when the output starting get a little bit more advanced.

Thanks,

1 Answers

Applying one level of shift transformation is enough such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "ScoreInfo": {
          "Team": {
            "*": {
              "HV": "&2.[&1].Source.&",
              "ID": "&2.[&1].Source.&",
              "@(0,ID)": "&2.[&1].ID"
            }
          }
        }
      }
    }
  }
]

where &2 s represent the outermost key name(Team), [&1] s are used to get rid of inner arrays rather to get objects, the & leaves (innermost ampersands) stand for getting the values of the current level of keys such as HV&ID

Related