0
votes

I am trying to filter out vales matching a variable using dataweave.

result = {
    "drives": [{
        "id": "0AEBByqXZ0xb4Uk9PVA",
        "name": "QA-zz-SFJobs-Contacts"
    }, {
        "id": "0AC_FdkeL63mHUk9PVA",
        "name": "QA"
    }]
}

above is my payload.

Am trying below code

%dw 2.0
output application/json
---
{ 
    drives: payload.result.drives[0] filter ((item, index) -> item.name == "QA") 
}

which gives me error

error:

You called the function 'Value Selector' with these arguments: 1: String ("{"drives":[{"id":"0AEBByqXZ0xb4Uk9PVA","name":"QA-zz-SFJobs-Contact...) 2: Name ("drives")

But it expects one of these combinations: (Array, Name) (Array, String) (Date, Name) (DateTime, Name) (LocalDateTime, Name) (LocalTime, Name) (Object, Name) (Object, String) (Period, Name) (Time, Name)

5| drives: payload.result.drives[0] filter ((item, index) -> item.name == vars.folderName) ^^^^^^^^^^^^^^^^^^^^^ Trace: at filter (line: 5, column: 13) at main (line: 5, column: 38)" evaluating expression: "%dw 2.0 output application/json

{ drives: payload.result.drives[0] filter ((item, index) -> item.name == vars.folderName) }".

Expected output:

{ "id": "0AC_FdkeL63mHUk9PVA", "name": "QA" }

how can i achieve this?

1
Assuming this is your original question : stackoverflow.com/questions/64784201/… , I have posted an answer there. Please check if that helpsBibek Kr. Bazaz

1 Answers

0
votes

The input should be a valid "JSON" like :

{
    "result": {
        "drives": [{
            "id": "0AEBByqXZ0xb4Uk9PVA",
            "name": "QA-zz-SFJobs-Contacts"
        },
        {
            "id": "0AC_FdkeL63mHUk9PVA",
            "name": "QA"
        }]
    }
}

Here applying the logic above :

{ 
    drives: payload.result.drives[0]
}

returns output as :

{
  "drives": {
    "id": "0AEBByqXZ0xb4Uk9PVA",
    "name": "QA-zz-SFJobs-Contacts"
  }
}

which is a single object containing object, and hence filter method cannot be applied as filter is applicable on arrays and arrays of objects.

To try your dataweave code snippet you can use the online dataweave editor : Online Dataweave Editor which can give you better insights.