0
votes

I am trying to extract formatted address, lat, lon using using google API. The JSON output looks like below:

var inpt = {
       "results" : [
          {
             "address_components" : [
                {
                   "long_name" : "94",
                   "short_name" : "94",
                   "types" : [ "street_number" ]
                },
                {
                   "long_name" : "Kinghorne Street",
                   "short_name" : "Kinghorne St",
                   "types" : [ "route" ]
                },
                {
                   "long_name" : "Goulburn",
                   "short_name" : "Goulburn",
                   "types" : [ "locality", "political" ]
                },
                {
                   "long_name" : "Goulburn Mulwaree Council",
                   "short_name" : "Goulburn Mulwaree",
                   "types" : [ "administrative_area_level_2", "political" ]
                },
                {
                   "long_name" : "New South Wales",
                   "short_name" : "NSW",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "Australia",
                   "short_name" : "AU",
                   "types" : [ "country", "political" ]
                },
                {
                   "long_name" : "2580",
                   "short_name" : "2580",
                   "types" : [ "postal_code" ]
                }
             ],
             "formatted_address" : "94 Kinghorne St, Goulburn NSW 2580, Australia",
             "geometry" : {
                "location" : {
                   "lat" : -34.742658,
                   "lng" : 149.722802
                },
                "location_type" : "ROOFTOP",
                "viewport" : {
                   "northeast" : {
                      "lat" : -34.7413090197085,
                      "lng" : 149.7241509802915
                   },
                   "southwest" : {
                      "lat" : -34.74400698029149,
                      "lng" : 149.7214530197085
                   }
                }
             },
             "place_id" : "ChIJ_57nYeiuFmsR0ZLPJl7b2P0",
             "plus_code" : {
                "compound_code" : "7P4F+W4 Goulburn, New South Wales, Australia",
                "global_code" : "4RQF7P4F+W4"
             },
             "types" : [ "establishment", "food", "point_of_interest", "store" ]
          }
       ],
       "status" : "OK"
    }

Using Dataweave of MuleSoft 4, I wanted to filter payload by "types" which is an Array. My expected result should be:

location: "New South Wales"

To get the above result I tried expression like below:

%dw 2.0
output application/json
--
{
    location: inpt.results.address_components filter ($.type contains "administrative_area_level_1")[0]."long_name"
}

But I am getting null result. Appreciate your help!

2

2 Answers

2
votes

Super close, your results is an array. If you have more than one JSON object in the array, you'll need to map them. Here's a solution to the current question:

%dw 2.0
output application/json
---
location: (inpt.results[0].address_components filter ($.types contains "administrative_area_level_1"))[0].long_name
0
votes

You can use this as well:

%dw 2.0
output application/json
---
{
    (flatten (payload.results.address_components) filter ($.types contains "administrative_area_level_1") map {
        location : $.long_name
    })
}