0

I am trying to select data from a JSON request based on one of the field

[
    {
        "Field1": "data1",
        "Field2": "set1",
        "Field3": "reset1"
    },
    {
        "Field1": "data2",
        "Field2": "set2",
        "Field3": "reset2"
    },
    {
        "Field1": "data3",
        "Field2": "set3",
        "Field3": "reset3"
    },
    {
        "Field1": "data4",
        "Field2": "set4",
        "Field3": "reset4"
    }
]

I want to select string value of field3 where field2 is "set3". That is "reset3".

DWL I tried:

payload.Field3 filter (payload.Field2 ==["set3"])
krlzlx
  • 5,752
  • 14
  • 47
  • 55

2 Answers2

1

You can filter like this:

<logger level="ERROR" message="Field3:::: #[(payload filter ($.Field2 =='set3'))[0].Field3]" />

Filter returns array since the array selector [0] if there is chance of more than one result you may want to change the expression.

Ryan Carter
  • 11,441
  • 2
  • 20
  • 27
  • This doesnot work i am getting NULL assigned to payload. However if i use setPayload after the transformation to extract the value then it is working fine. As good practice i want to handle this in Dataweave code... – Nishith Coomar Jul 19 '18 at 16:48
  • that is dataweave code. that is just a logger to print the value for demo. use set-payload etc if you want to set the value somewhere – Ryan Carter Jul 19 '18 at 21:46
0

Use can use this DW expression:

%dw 1.0
%output application/json
---
payload filter ($.Field2=="set3") map {
    Field3: $.Field3
}

Result:

[
   {"Field3": "reset3"}
]
Subham Pal
  • 46
  • 1