4
votes

I'm trying to understand the behaviour of DataWeave v1.0 when it comes to mapping objects in a root JSON array.

At this stage I just want to map each item in the array as-is without mapping each individual field of the item. I need to do it for each item in the array because later on I want to edit some of the fields, but since there are potentially many I don't want the overhead of mapping them one-by-one.

This is my dataweave:

%dw 1.0
%output application/json
---
payload map {
    ($)
}

This is my input:

[
  {
    "MyString": "ABCD",
    "MyNumber": 123,
    "AnObject": {
       "MyBool": false,
       "MyNestedObject": {
            "MyNestedString": "DEF"
       }
    }
  }
]

I want my output to be (at this stage) exactly the same as my input.

Instead my (wrong) output is:

[
  {
    "MyString": "ABCD",
    "MyNumber": 123,
    "MyBool": false,
    "MyNestedObject": {
      "MyNestedString": "DEF"
    }
  }
]

As you can see the object AnObject is missing, although its children remain.

Things are worse if the input includes arrays, for example the input:

[
  {
    "MyString": "ABCD",
    "MyNumber": 123,
    "AnObject": {
       "MyBool": false,
       "MyNestedObject": {
            "MyNestedString": "DEF"
       }
    },
    "AnArray": [
        {
            "Title": "An array item",
            "Description": "Pretty standard"
        }
    ]
  }
]

Throws the error:

Cannot coerce a :array to a :object.

I have played around with the mapObject operation on the root array items too, but I always run into the same behaviour. Is anyone able to explain what is happening here, and show me how I can copy each item in the root payload across dynamically.

Mule runtime is 3.9.1.

1

1 Answers

1
votes

To go through each item in the array and let them as it is, you should do payload map $, that is the same as payload map ((item) -> item)

What you were doing is the same as: payload map ((item) -> {(item)}).

Here what you are returning for each item is the expression {(expr)} that in the DW version that runs on Mule 3.9.1, it has an accidental behavior where the expression tries to coerce expr (that in this case is an object) to array of objects and then it will try to flatten all the objects in that coerced array inside the parent object. It looks like is trying to coerce the value of the keys too, that's why DW throws the error.

This behavior of {()} changes in newer versions of DW.