How to remove or add nested fields with Dataweave

Posted by

How to remove or add nested fields with Dataweave

How to remove or add nested fields with Dataweave from your payload and or variables? This article will teach you how to do that in simple and immediate steps!

To remove or add fields, which are nested into your payload or variables according to the situation, could be a frustrating process… If you don't know how to remove or add them with Dataweave.

In another article, we talked about how to remove fields from your contents thanks to Dataweave if you know the key. For instance, having the following payload:

{
  "field_one": "one",
  "field_two": "two"
}

If you're willing to remove the field named "field_two" from the payload, you need to act like this:

 

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

As I was saying a couple of paragraph before, the scenario might be more frustrating if fields are nested.

Given the following payload, imagine you’re willing to remove the field named “nested_to_remove”:

 

{
    "field1": "Hello world!",

    "first_level": {
        "nested_one": "Hi",
        "nested_to_remove": "remove me!"
    }
}

 

You cannot act like before, but I'm here to help 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 remove nested fields with Dataweave

To remove nested fields, you cannot use the direct operation we saw before… More or less.

You can surely use the mapObject function iterating over the fields and combine the object itself once again, but… There's a more convenient way to act.

You can remove fields directly from the object, which means you can save the nested fields apart and afterward add them back again! …Of course, without what you're willing to remove.

The first step is to store the nested fields into a variable, creating an object with the exact one you're willing to edit as the main field.

In our case, it is the "first_level," but it's not enough: you must remove the desired fields. 

How?

Like so:

%dw 2.0
output application/json

var subfields = {"first_level": payload."first_level" - "nested_to_remove"}

You can see that you have re-created the nested object, but its fields are to be removed!

In conclusion, we need to re-create the original payload but the nested fields you're willing to remove.

How?

Combining the two steps:

%dw 2.0
output application/json

var subfields = {"first_level": payload."first_level" - "nested_to_remove"}
---

(payload - "first_level") ++ subfields

You remove the original nested fields, then immediately after, you add the original nested ones without the desired areas to be deleted.

Indeed, that's the outcome of our payload's example:

 
{
  "field1": "Hello world!",
  "first_level": {
    "nested_one": "Hi"
  }
}​

Now that you understand how to remove nested fields with Dataweave let's know the opposite operation.

 

How to add nested fields to your content with Dataweave

According to the "game", we implemented so far in terms of remove-then-add, there's nothing as different as already seen before.

What's the difference if you're willing to add nested fields with Dataweave instead of removing them?

Simple: you'll add them to the nested part of the payload, then remove and add them back as we did before.

For instance:

%dw 2.0
output application/json

var subfields = {"first_level": payload."first_level" ++ {"nested_to_be_add": "value"}}
---

(payload - "first_level") ++ subfields

The related outcome is as follows:

{
  "field1": "Hello world!",
  "first_level": {
    "nested_one": "Hi",
    "nested_to_remove": "remove me!",
    "nested_to_be_add": "value"
  }
}