1

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"
  }
]

3 Answers 3

2

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"
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, I am getting error when I run the same. Message : "You called the function 'filter' with these arguments: 1: String ("" as String {encoding: "UTF-8", mediaType: "application/json; charset=UTF-8"...) 2: Function (($:Any, $$:Any) -> ???) But it expects arguments of these types: 1: Array 2: Function
It looks like in your case it is receiving as payload an empty string instead of the array, (..."" as String...). How are you testing it?
Yeah I got the issue, payload was going as null, so it was failing. There is a csv file which has some name value pair. I want to fetch value on the basis of name. Can you help with this.
Please open a new question for the CSV issue and provide the details.
0

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"
]

Comments

0

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

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.