0

So I have this json retrieved from the Google Maps API and I just want to get longitude and latitude. I am looking to use the jolt template to extract just the information that I need.

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

So this is the jolt spec that I am using:

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

I would like to retrieve a json that looks like this:

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

But I keep getting null... Any help would be appreciated thanks

TofferJ
  • 4,678
  • 1
  • 37
  • 49
harry2000
  • 1
  • 1

1 Answers1

1

Your original spec wasn't working because "lat": "employees[&1].firstName" should be "lat": "employees[&3].firstName".

In this case &1 evaluated to the word "location". &3 gets you up the tree to the index of the input results array, which is what I think you meant.

Shift maintains a stack as it doing its transform, the & wildcard lets you grab previously matched values from the stack / up the tree.

From where "lat" is in the spec, it is 4 levels up the stack 0,1,2,3 to get to the index of the results array, that was matched by the *.

Spec

[
  {
    "operation": "shift",
    "spec": {
      "results": {
        "*": {
          "geometry": {
            "location": {
              "lat": "data[&3].lat",
              "lng": "data[&3].lng"
            }
          }
        }
      }
    }
  }
]
Milo S
  • 4,466
  • 1
  • 19
  • 22