Dataweave how to handle specific key from an object

Dataweave how to handle specific key from an object

Are you willing to handle a specific key from an object? Given a JSON object, you’re eager to remove a particular key? Otherwise, are you attempting to rename it? What if you’re willing to transform an array object attribute into a string? In this article, I would like to share my “How(s) to” with you.

My name is Lorenzo Neri, a Cap4 Lab integration engineer. Several times during my working time, I met many different problems I tried to solve, then afterward, a question came to my mind: “What if I bring help to other people to solve my same problem?”.

Let’s get started.

How to handle specific key from an object

Before getting into detailed operations, the first step is the main built-in DataWeave function we need. I’m talking about the mapObject function. You might know that this function iterates over the values, attributes, or indexes of the object passed as the function’s argument.

With this function, we can manipulate every single key that builds up our object, which means that we will be able to remove, alter, and even apply more complex functions such as array operations.

We need an additional step: how could we identify a specific key from an object? Yes, you got it: selection.

Through the if-then-else structure, we get control over mapObject’s iteration.

Let’s get further with an example, given this object:

{
    "name": "Marc",
    "surname": "Green",
    "numbers": [1,2,3,4,5,6]
}

We want to rename the “surname” field into “Green” and set its corresponding value to “bla”.

To do like so, the following code is what we need:

%dw 2.0
output application/json
---
payload mapObject ((value, key, index) -> 
        if ( key == "surname" )
            "Green": "bla"
        else
            (key): value
    )

The “else” part is mandatory in Dataweave. In this particular case, we only need to re-write the object as is so that we need to act over the “surname” key, as we said before.

If you need a real-time Dataweave editor, output included, you could look over the Dataweave Playground official one.

Before getting into the next part, I would like to share additional and non-immediate details about handling specific keys from an object.

The previous chunk-code works “perfectly”: it has no syntax errors, hasn’t it?

With complex JSON objects, it sometimes happens to face unexpected syntax errors. This is because of the Dataweave nature.

To solve it, it’s sufficient to force the object’s key casting to String, like so:

%dw 2.0
output application/json
---
payload mapObject ((value, key, index) -> 
        if ( (key as String) == "surname" )
            "Green": "bla"
        else
            (key): value
    )

Now, we could proceed to the next step.

How to remove a specific key from an object with Dataweave

If you are willing to handle a particular key from an object with Dataweave, now that you know how to change it, you wonder how to remove a specific key from that object, right?

Considering the previous object, let’s assume that you’re after removing the “name” field.

Easy peasy, I could say 😃

%dw 2.0
output application/json
---
payload - "name"

As soon as you know which key are you willing to remove, the general syntax is:

object_youre_willing_to_edit - "nameOfTheFieldToRemove"

to conclude, let’s see the final point.

How to transform a specific complex object key with Dataweave

Would you like to convert an array valued object key into a string? What you saw in the first point of this article can be enriched with additional functions.

To keep going with the primary example, let’s say that you would like to transform the “numbers” field, which is an array, into a single string.

What do you need?

Nothing as so special as you might think:

%dw 2.0
output application/json
---
payload mapObject ((value, key, index) -> 
        if ( (key as String) == "numbers" )
            (key): joinBy(value,",")
        else
            (key): value
    )