1
votes

I have a JSON input:

{
  abc: "",
  def: "hello"

}

I want to make this blank element as nillable in XML i.e. . I am using the below dataweave code:

%dw 2,0
output application/xml skipNullOn="everywhere"
var makeNil= (in) ->
in match {
case is Array -> in map makeNil($)
case is Object -> in mapObject (
if ( ($) == "")
  ($$) @(xsi#'nil':true): {}
else ($$): makeNil($)
)
else -> in
}
---
makeNil(payload)

I am not able to create an attribute using @(xsi#'nil':true) for key($$). Please help me

1
There are some errors that need to be fixed before the script even executes: the keys in the JSON payload miss quotes, it is an invalid JSON. The version separator is a dot, not a comma, and the xsi namespace is not defined. What is the root element of the output? Provide an example of the expected output, and the actual results or error. - aled

1 Answers

1
votes

Solving the errors that I mentioned in my comment, adding a root element works. Remember that XML unlike JSON requires a root element.

%dw 2.0
output application/xml skipNullOn="everywhere"
ns xsi http://www.w3.org/2001/XMLSchema-instance
var makeNil= (in) ->
    in match {
        case is Array -> in map makeNil($)
        case is Object -> in mapObject (
            if ( ($) == "")
                ($$) @(xsi#'nil':true): {}
            else ($$): makeNil($)
        )
        else -> in
    }
---
top: makeNil(payload)

input:

{
  "abc": "",
  "def": "hello"
}

output:

<?xml version='1.0' encoding='UTF-8'?>
<top>
  <abc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
  <def>hello</def>
</top>