Search code examples
muledataweavemulesoftmule4

Dataweave: How to concatenate values for a certain key in json array


My input is

[
  {
    "Id": 5,
    "FirstName": "ALEX",
    "LastName": "JOHNSON"
  },
  {
    "Id": 4,
    "FirstName": "BOB",
    "LastName": "BROWN"
  },
  {
    "Id": 2,
    "FirstName": "JANE",
    "LastName": "DOE"
  },
  {
    "Id": 1,
    "FirstName": "JOHN",
    "LastName": "SMITH"
  },
  {
    "Id": 6,
    "FirstName": "JOHN",
    "LastName": "WILKINS"
  },
  {
    "Id": 3,
    "FirstName": "TIMOTHY",
    "LastName": "WALTERS"
  }
]

Output I want is a string concatenating all the FirstName values in the order in which they are listed in the input

"ALEX, BOB, JANE, JOHN, JOHN, TIMOTHY"

I'm new to Dataweave and not sure how to do this
Thanks in advance


Solution

  • You can also try it with the use of descendant selector

    %dw 2.0
    output application/json
    ---
    payload..FirstName joinBy ","