saurabh0409
saurabh0409

Reputation: 13

Mule dataweave transformation

I have below json as my input payload and I want to fetch groupvalue where groupname is b. How to do this in dataweave ?

[
  {
    "groupName": "a",
    "groupvalue": "1234"
  },
  {
    "groupName": "b",
    "groupvalue": "7890"
  }
]

Upvotes: 1

Views: 151

Answers (3)

Sandeep Sai Kumar
Sandeep Sai Kumar

Reputation: 16

In case, there are a number of key, value pairs for groupName "b", this code will give you an array of the groupvalue for groupName "b"s.

%dw 2.0
output application/json
---
(payload filter $.groupName =="b").*groupvalue

example:-

payload = [
  {
    "groupName": "a",
    "groupvalue": "1234"
  },
  {
    "groupName": "b",
    "groupvalue": "7890"
  },
  {
    "groupName": "b",
    "groupvalue": "8330",
    "groupvalue": "1234"
  }
]

example 2

payload = [
  {
    "groupName": "a",
    "groupvalue": "1234"
  },
  {
    "groupName": "b",
    "groupvalue": "7890"
  },
  {
    "groupName": "b",
    "groupvalue": "8330"
  }
]

script:-

%dw 2.0
output application/json
---
(payload filter $.groupName =="b")..groupvalue

Upvotes: 0

tleo
tleo

Reputation: 361

In case, there are a number of key, value pairs for groupName "b", this code will give you an array of the groupvalue for groupName "b"s.

%dw 2.0
output application/json
---
(payload filter ((item, index) -> item.groupName == "b"))["groupvalue"]

For example:

    payload = [
  {
    "groupName": "a",
    "groupvalue": "1234"
  },
  {
    "groupName": "b",
    "groupvalue": "7890"
  },
  {
    "groupName": "b",
    "groupvalue": "8330"
  }
]

You will get:

[
  "7890",
  "8330"
]

Upvotes: 0

aled
aled

Reputation: 25872

If you are absolutely sure there is only one element in the array with groupName equal to "b":

%dw 2.0
output application/json
---
(payload filter ($.groupName == "b") map ( $.groupvalue)) [0]

With your input I get the following output:

"7890"

Upvotes: 2

Related Questions