首页 > 解决方案 > dataweave fetch first char from array elements

问题描述

I am trying to see if any Types.Flag starts with 'X', if true, I need to set 'subscribed' to true.

Input:

{
    "Subscribers": [{
            "PhoneNumber": "9876543210",
            "Types": [{
                    "Name": "abcd",
                    "Flag": "WIR"
                },
                {
                    "FilterName": "efg",
                    "Flag": "XNJ"
                },
                {
                    "FilterName": "hijk",
                    "Flag": "YIR"
                }
            ]
        },
        {
            "PhoneNumber": "9823456789",
            "Types": [{
                "FilterName": "lmn",
                "Flag": "MST"
            }]
        }
    ]
}

Expected output

{
  "subscriberList": [
    {
      "phoneNumber": "2012020004",
      "subscribed": true
    },
    {
      "phoneNumber": "2234567890",
      "subscribed": false
    }
  ]
}

Could you please suggest on how this could be achieved?

标签: dataweave

解决方案


Try this:

%dw 2.0
output application/json

import some from dw::core::Arrays

var data = {
    "Subscribers": [{
            "PhoneNumber": "9876543210",
            "Types": [{
                    "Name": "abcd",
                    "Flag": "WIR"
                },
                {
                    "FilterName": "efg",
                    "Flag": "XNJ"
                },
                {
                    "FilterName": "hijk",
                    "Flag": "YIR"
                }
            ]
        },
        {
            "PhoneNumber": "9823456789",
            "Types": [{
                "FilterName": "lmn",
                "Flag": "MST"
            }]
        }
    ]
}
---
subscriberList: data.Subscribers map {
    phoneNumber: $.PhoneNumber,
    subscribed: $.Types.*Flag some (e) -> e startsWith "X"
}

Here's the documentation of the functions I made use of:


推荐阅读