MuleSoft JSON 数组过滤器

MuleSoft JSON Array Filter

我正在尝试使用 google API 提取格式化的地址、纬度、经度。 JSON 输出如下所示:

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

使用 MuleSoft 4 的 Dataweave,我想通过数组 "types" 过滤有效载荷。我的预期结果应该是:

location: "New South Wales"

为了获得上述结果,我尝试了如下表达式:

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

但我得到的结果为空。感谢你的帮助!

超级接近,你的结果是一个数组。如果数组中有多个 JSON 对象,则需要 map 它们。这是当前问题的解决方案:

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

你也可以用这个:

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