MuleSoft DataWeave: How to Extract IDs from a JSON Array

Welcome back to the DataWeave journey! If you are building APIs in MuleSoft, one of the most common tasks you will encounter is Extraction. Specifically, you often need to grab a simple list of values from a complex array of objects.


Have you ever stared at a massive JSON response from an upstream system and thought, "I don't need all this noise, I just need the IDs to pass to the next system"? If so, this guide is for you. Today, we will explore two powerful ways to achieve this: the "Shortcut" (Direct Access) and the "Power User" method (Map).

The Problem Statement

Imagine an upstream system sends you a list of user profiles. The payload is an array of objects, and each object contains an id and a name.

Your Goal: You need to strip away the names and structural overhead, leaving only a clean list (array) of the ID numbers.

Input JSON

Here is the sample data we are working with:

[
      { "id": 1, "name": "Archer" },
      { "id": 2, "name": "Cyril"  },
      { "id": 3, "name": "Pam"    }
    ]

Method 1: The "Grab-and-Go" (Direct Field Access)

The beauty of DataWeave is its built-in intelligence. It understands that if you ask for a specific field on an array (list), you probably want that field extracted from every item in that array. We call this the Direct Reference method.

It is concise, clean, and perfect for simple extractions where no transformation is needed.

The DataWeave Script

%dw 2.0
    output application/json
    var myPayload = [
      { "id": 1, "name": "Archer" },
      { "id": 2, "name": "Cyril"  },
      { "id": 3, "name": "Pam"    }
    ]
    ---
    myPayload.id

Why This Works

  • Variable Definition: We stored our sample data in a variable named myPayload.
  • The Magic Selector: By simply typing myPayload.id, DataWeave automatically looks inside the array. It sees that the key id exists in the objects inside, iterates over them, and returns an array containing only the values of those IDs.

Method 2: The "Power User" (Using the Map Function)

Sometimes, you need more control. You might need to change the data type of the ID (e.g., number to string) or combine it with another field during extraction. For this, we use the map function.

Think of map as an explicit loop. You are telling DataWeave: "Go through every item, one by one, and do exactly what I say."

The DataWeave Script

%dw 2.0
    output application/json
    var myPayload = [
      { "id": 1, "name": "Archer" },
      { "id": 2, "name": "Cyril"  },
      { "id": 3, "name": "Pam"    }
    ]
    ---
    myPayload map($.id)

Why This Works

  • Explicit Iteration: The map keyword triggers a loop over the array.
  • The Context ($): Inside the map function, the dollar sign ($) represents the current object being processed (e.g., { "id": 1, "name": "Archer" }).
  • Extraction: By writing $.id, we are explicitly telling the script to grab the ID property from the current object in the loop.

Conclusion

Both methods detailed above will produce the exact same result:

[1, 2, 3]

So, which one should you choose?

  • Use Solution 1 (Direct Access): When you want clean, readable code and simply need to extract a raw value without changing it.
  • Use Solution 2 (Map): When you are building complex logic and might need to transform that ID (e.g., convert it to a String or add a prefix like "ID-" ++ $.id) during the extraction process.

Mastering the difference between these two puts you on the fast track to writing robust MuleSoft integration solutions!

Post a Comment (0)
Previous Post Next Post