Jolt与复杂数据json

时间:2018-02-06 07:04:50

标签: json jolt

我有一个json,我想用jolt从中提取另一个json。使用JOLT转换演示网站,我能够投入一些规格,但它并没有给我我想要的精确json。

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

我想将其转换为

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

到目前为止我能搞清楚的规格是

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

此规范足够接近但不准确。它会生成

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

任何提示都将非常感激。

1 个答案:

答案 0 :(得分:0)

规格

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