How to create a Jolt specification

Viewed 203

I am having issues to create a Jolt specification for the following input, I want to convert the Score to an array called Scores, what is happening is when I have only one score it is not send as array, so I want to convert to an array of scores.

[
  {
    "Page": 320301,
    "ScoreInfo": {
      "StadiumID": 126,
      "Stadium": "abcded",
      "Inn": 3,
      "TB": 2,
      "Team": [
        {
          "HV": 1,
          "ID": 1,
          "NameS": "abdcd",
          "NameES": "abdcd",
          "R": 1,
          "PA": 10,
          "H": 2,
          "BB": 3,
          "SB": 0,
          "E": 0,
          "Score": {
            "Inn": 1,
            "TB": 2,
            "R": 0,
            "H": 0,
            "BB": 1
          }
        },
        {
          "HV": 2,
          "ID": 3,
          "NameS": "afewqe",
          "NameES": "dadfa",
          "R": 1,
          "PA": 12,
          "H": 3,
          "BB": 0,
          "SB": 0,
          "E": 0,
          "Score": [
            {
              "Inn": 1,
              "TB": 1,
              "R": 1,
              "H": 2,
              "BB": 0
            },
            {
              "Inn": 2,
              "TB": 1,
              "R": 0,
              "H": 0,
              "BB": 0
            }
          ]
        }
      ],
      "HV": null
    }
  }
]

The output expected is:

{
  "ScoreInfo" : {
    "Team" : [ {
      "HV" : 1,
      "ID" : 1,
      "NameS" : "abdcd",
      "NameES" : "abdcd",
      "R" : 1,
      "PA" : 10,
      "H" : 2,
      "BB" : 3,
      "SB" : 0,
      "E" : 0,
      "Scores" : [ {
        "Inn" : 1,
        "TB" : 2,
        "R" : 0,
        "H" : 0,
        "BB" : 1
      } ]
    }, {
      "HV" : 2,
      "ID" : 3,
      "NameS" : "afewqe",
      "NameES" : "dadfa",
      "R" : 1,
      "PA" : 12,
      "H" : 3,
      "BB" : 0,
      "SB" : 0,
      "E" : 0,
      "Scores" : [ {
        "Inn" : 1,
        "TB" : 1,
        "R" : 1,
        "H" : 2,
        "BB" : 0
      }, {
        "Inn" : 2,
        "TB" : 1,
        "R" : 0,
        "H" : 0,
        "BB" : 0
      }]
    } ]
  },
  "Type" : "ScoreInfo",
  "Datasource" : "Vendor"
}

I have tried it so far, but it is not going anywhere:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "ScoreInfo": {
          "Team": {
            "*": {
              "@": "ScoreInfo.&2",
              "Score": {
                "*": {
                  "@": "ScoreInfo.Team.Scores.[]"
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "Datasource": "Vendor",
      "Type": "ScoreInfo"
    }
  }
]

Thanks in advance,

1 Answers

The common key name(Score) leads to a confusion, so need to seperate the first(0) index of the Team array and the other members while evaluation, and use [] extension only for the first index at the end of the "Score": "&3.&2.[#2].&s[]" key-value pair

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "ScoreInfo": {
          "Team": {
            "0": {
              "*": "&3.&2.[#2].&",
              "Score": "&3.&2.[#2].&s[]"
            },
            "*": {
              "*": "&3.&2.[#2].&",
              "Score": "&3.&2.[#2].&s"
            }
          }
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "Datasource": "Vendor",
      "Type": "ScoreInfo"
    }
  }
]

where no need to explicitly write the name of the keys, you can rather use ampersand placeholders along with integer suffixes(such as &2,&3..) in order to grab the related value from the outer levels, or just a single ampersand(&) to grab the current value of the key.

enter image description here

Related