0

I have a below json, from which I want to extract the another json using jolt. Using JOLT transform demo website, I am able to coin some spec but it doesnt gives me the exact json I want.

{
  "status": "OK",
  "recordCount": 26,
  "startTimestamp": "2017-08-08T04:49:31.3860406Z",
  "endTimestamp": "2017-08-08T04:49:31.8860442Z",
  "timeTaken": 0.5000036,
  "apiResults": [
    {
      "sportId": 28,
      "name": "Olympics",
      "league": {
        "leagueId": 442,
        "name": "Winter Olympics",
        "abbreviation": "WNTR_OLY",
        "displayName": "Winter Olympics",
        "season": {
          "season": 2014,
          "isActive": null
        },
        "medals": [
          {
            "olympicCountry": {
              "countryId": 1000,
              "name": "Russian Federation",
              "abbreviation": "RUS"
            },
            "medalCount": {
              "gold": 13,
              "silver": 11,
              "bronze": 9,
              "total": 33
            }
          },
          {
            "olympicCountry": {
              "countryId": 8673,
              "name": "Russian Federation",
              "abbreviation": "RUS"
            },
            "medalCount": {
              "gold": 13,
              "silver": 11,
              "bronze": 9,
              "total": 33
            }
          }
        ]
      }
    }
  ]
}

I want to transform it in to

{
  "data" : [
    {
    "countryCode" :  1000,
    "countryName" : "Russian Federation",
    "medals" :  {
      "gold" : 13,
      "silver" : 11,
      "bronze" : 9,
      "total" : 33
    }
    }, 
    {
      "countryCode": 8673,
      "countryName": "Russian Federation",
      "medals": {
      "gold" : 13,
      "silver" : 11,
      "bronze" : 9,
      "total" : 33
    }
    }
  ]
}

The spec so far i could figure out is

[
  {
    "operation": "shift",
    "spec": {
      "apiResults": {
        "*": {
          "league": {
            "medals": {
              "*": {
                "olympicCountry": {
                  "countryId": "data.countryCode",
                  "name": "data.countryName"
                },
                "medalCount": "data.medals"
              }
            }
          }
        }
      }
    }
  }
]

This spec is close enough but not accurate. It generates

{
  "data" : {
    "countryCode" : [ 1000, 8673 ],
    "countryName" : [ "Russian Federation", "Russian Federation" ],
    "medals" : [ {
      "gold" : 13,
      "silver" : 11,
      "bronze" : 9,
      "total" : 33
    }, {
      "gold" : 13,
      "silver" : 11,
      "bronze" : 9,
      "total" : 33
    } ]
  }
}

Any hint will be much appreciated.

plzdontkillme
  • 1,497
  • 3
  • 20
  • 38

1 Answers1

0

Spec

[
  {
    "operation": "shift",
    "spec": {
      "apiResults": {
        "*": {
          "league": {
            "medals": {
              "*": {
                "olympicCountry": {
                  "countryId": "data[&2].countryCode",
                  "name": "data[&2].countryName"
                },
                "medalCount": "data[&1].medals"
              }
            }
          }
        }
      }
    }
  }
]
Milo S
  • 4,466
  • 1
  • 19
  • 22
  • That worked like a charm.Thankts @Milo S. I am intending it to use more often, is there documentation to understand how it works in more detail ? – plzdontkillme Feb 07 '18 at 18:54