【问题标题】:MuleSoft JSON Array FilterMuleSoft JSON 数组过滤器
【发布时间】:2019-12-13 10:19:14
【问题描述】:

我正在尝试使用 google API 提取格式化地址、lat、lon。 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"
}

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

【问题讨论】:

    标签: mule mule-studio dataweave mulesoft


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您也可以使用它:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-14
        • 1970-01-01
        • 2022-11-28
        • 2017-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多