0

I am trying to match the specific value from input payload using dataweave.

Input:

{
    "drives": [{
        "id": "0AEzOyzyCb7Uk9PVA",
        "name": "SFJob-2020-10"
    }, {
        "id": "0AMEHi1wsq-8FUk9PVA",
        "name": "SFJobs-2020-11"
    } ],
    "nextPageToken": "~!!~AI9FV7RV4uSXy20zpCBTP2LFWCXS0c"
},
{
    "drives": [{
        "id": "0AEz3mOyzyCb7Uk9PVA",
        "name": "Dev2020-10"
    }, {
        "id": "0AMEHi1wsq-8FUk9PVA",
        "name": "Dev2020-11"
    }],
"nextPageToken": "~!!~AI9P2LFWCXS0c"
}

how can i check whether value "Dev2020-10" is present or not.

i am using below code giving me error.

%dw 2.0
output application/json
---
payload.drives filter ((item, index) -> item.name == 'Dev2020-10')

Expected output:

[ {

"id": "0AEz3mOyzyCb7Uk9PVA",
"name": "Dev2020-10"

} ]

How can i achieve this?

2 Answers 2

2

First, the input is not valid. I assume that it is supposed to be a JSON array so I enclosed into brackets: [ {..},{..} ].

After that you will notice that the filter is expecting a single object, but because it is an array you need to map it first. To return a single array I used a flatMap():

%dw 2.0
output application/json
---
payload flatMap $.drives filter ((item, index) -> item.name == 'Dev2020-10')

Output:

[
  {
    "id": "0AEz3mOyzyCb7Uk9PVA",
    "name": "Dev2020-10"
  }
]
Sign up to request clarification or add additional context in comments.

Comments

1

The solution can be thought of two steps:

  1. Merge all the drives together to get a single array of objects flatten(inputArray.drives)
  2. Filter the new array of objects based on the search criteria filter ((item, index) -> item.name == "Dev2020-11")

The solution will be this :

%dw 2.0
output application/json

var inputArray = [
    {
    "drives": [{
        "id": "0AEzOyzyCb7Uk9PVA",
        "name": "SFJob-2020-10"
    }, {
        "id": "0AMEHi1wsq-8FUk9PVA",
        "name": "SFJobs-2020-11"
    } ],
    "nextPageToken": "~!!~AI9FV7RV4uSXy20zpCBTP2LFWCXS0c"
    },
    {
    "drives": [{
        "id": "0AEz3mOyzyCb7Uk9PVA",
        "name": "Dev2020-10"
    }, {
        "id": "0AMEHi1wsq-8FUk9PVA",
        "name": "Dev2020-11"
    }],
"nextPageToken": "~!!~AI9P2LFWCXS0c"
    }
]
---
flatten(inputArray.drives) filter ((item, index) -> item.name == "Dev2020-11") 

To learn more about flatten method, refer the documentation : mule 4 doc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.