坚持简单的颠簸转换

时间:2018-06-15 14:45:59

标签: jolt

所以我从Google Maps API中检索了这个json,我只想获得经度和纬度。我希望使用jolt模板来提取我需要的信息。

{
  "results": [
    {
      "address_components": [
        {
          "long_name": "1115",
          "short_name": "1115",
          "types": [
            "street_number"
          ]
        },
        {
          "long_name": "West Idaho Avenue",
          "short_name": "W Idaho Ave",
          "types": [
            "route"
          ]
        },
        {
          "long_name": "Ontario",
          "short_name": "Ontario",
          "types": [
            "locality",
            "political"
          ]
        },
        {
          "long_name": "Malheur County",
          "short_name": "Malheur County",
          "types": [
            "administrative_area_level_2",
            "political"
          ]
        },
        {
          "long_name": "Oregon",
          "short_name": "OR",
          "types": [
            "administrative_area_level_1",
            "political"
          ]
        },
        {`enter code here`
          "long_name": "United States",
          "short_name": "US",
          "types": [
            "country",
            "political"
          ]
        },
        {
          "long_name": "97914",
          "short_name": "97914",
          "types": [
            "postal_code"
          ]
        },
        {
          "long_name": "2146",
          "short_name": "2146",
          "types": [
            "postal_code_suffix"
          ]
        }
      ],
      "formatted_address": "1115 W Idaho Ave, Ontario, OR 97914, USA",
      "geometry": {
        "location": {
          "lat": 44.0294445,
          "lng": -116.9776502
        },
        "location_type": "ROOFTOP",
        "viewport": {
          "northeast": {
            "lat": 44.03079348029149,
            "lng": -116.9763012197085
          },
          "southwest": {
            "lat": 44.02809551970849,
            "lng": -116.9789991802915
          }
        }
      },
      "partial_match": true,
      "place_id": "ChIJP3C3Z6uPr1QRUDkcSIXzx5g",
      "types": [
        "establishment",
        "point_of_interest",
        "school"
      ]
    }
  ],
  "status": "OK"
}

所以这是我正在使用的颠簸规范:

[
  {
    "operation": "shift",
    "spec": {
      "results": {
        "*": {
          "geometry": {
            "location": {
              "lat": "employees[&1].firstName",
              "lng": "employees[&1].lastName"
            }
          }
        }
      }
    }
  }
]

我想检索一个看起来像这样的json:

{
  "data" : [ 
  {
     "lng": "-116.9763012197085",
     "lat": "44.0294445"
  } ]
}

但我一直都是空的......感谢任何帮助,谢谢

1 个答案:

答案 0 :(得分:0)

您的原始规范不起作用,因为"lat": "employees[&1].firstName"应为"lat": "employees[&3].firstName"

在这种情况下,&1评估了单词" location"。 &3让你把树带到输入results数组的索引,这就是我认为你的意思。

Shift在进行转换时保持堆栈,&通配符允许您从堆栈中向上/从树中获取先前匹配的值。

从规范中的"lat"开始,它是堆栈0,1,2,3上的4个级别,以获得结果数组的索引,它与*匹配。

规格

[
  {
    "operation": "shift",
    "spec": {
      "results": {
        "*": {
          "geometry": {
            "location": {
              "lat": "data[&3].lat",
              "lng": "data[&3].lng"
            }
          }
        }
      }
    }
  }
]