MuleSoft JSON 数组过滤器

霍舍阿拉姆

我正在尝试使用谷歌 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,我想通过“类型”这是一个数组来过滤有效负载。我的预期结果应该是:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章