Skip to main content

Hello,

I am currently working on building certain JSON messages.

For doing this I am starting with an XML message that has the desired structure and place the data into this XML. Then I use the JSON Connector to convert my XML into the JSON format.

So far so good.

Due to the fact that sometimes the data of a node does not exist, I would like to know, if there is an option in the JSON Connector that will automatically remove all nodes that are empty in the underlying XML.

For example the following XML Structure shows that some fields have values and other do not:
<?xml version="1.0" encoding="UTF-8"?>

<Items xmlns:json="http://arc.cdata.com/ns/jsonconnector" json:array="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <Item>

    <field1>value1</field1>

    <field2>value1</field2>

    <field3></field3>

    <subnode1>

      <subfield1>subvalue1</subfield1>

      <subfield2></subfield2>

    </subnode1>

    <subnode2>

      <subfield3></subfield3>

      <subfield4></subfield4>

    </subnode2>

    <field4>value4</field4>

  </Item>

</Items>

When sending this through the JSON Connector the result looks like this:
n

  {

    "field1": "value1",

    "field2": "value1",

    "field3": "",

    "subnode1": {

      "subfield1": "subvalue1",

      "subfield2": ""

    },

    "subnode2": {

      "subfield3": "",

      "subfield4": ""

    },

    "field4": "value4"

  }

]

What I want to have would look like this, not including any of the empty fields or substructures:
v

  {

    "field1": "value1",

    "field2": "value1",

    "subnode1": {

      "subfield1": "subvalue1"

    },

    "field4": "value4"

  }

]

I know that I could start adding Conditions to each of the XML Nodes in my XMLMap Connector so that the result would only have the XML nodes that are not empty.
But when it comes to large structures with many fields this is pretty time consuming.

Hence my question is if there is an option in the JSON connector (or any other possibility) to globally remove empty nodes from the JSON.

Thanks for any help or tip.

 Gordon

Hi @gordan

Before converting your XML files to JSON, you can use the XML Map connector. Implement conditional mapping within the XML Map to check if the nodes are not empty and only then map them. This will ensure that empty nodes are removed in the XML file itself. Then, you can use the JSON connector to convert these files to JSON.

 

On the destination side, hover over the element and then click on 'Conditional Mapping.

 

 

Apply the 'If Not Empty' condition. This will ensure that only valid nodes with values are mapped

 

 

I have also attached the connector arc flow for your review.

Please refer to this links for more information: https://cdn.cdata.com/help/AZK/mft/XML-Map-Conditionals.html

 


Hello Rohit,

maybe you haven’t seen the following sentence, I was writing initially:

“I know that I could start adding Conditions to each of the XML Nodes in my XMLMap Connector so that the result would only have the XML nodes that are not empty.“

But, you know, having files with 50+ nodes and subnodes in multiple levels, this is a very painful task, because this Condition needs to be added to every single field separately and is getting worse when it comes to defining Conditions for parent nodes (checking, if any of the subnodes is filled).

That is why I was asking for a more global approach, something like for example a clever option “RemoveEmptyNodes=TRUE” in the “Other Settings” field of the JSON Connector or a smart way of running through all nodes in the XML and removing all empty ones.

Thanks

  Gordon


Unfortunately, there is no configuration setting in the JSON connector that would automatically omit empty XML elements from the transformation for that connector - both the empty string and null values have existing definitions in JSON, and the connector anticipates both. 

 

Is the source that is consuming this JSON file unable to deal with the empty elements? Note that in an XML Map, it is possible to configure the output of an empty node to be treated as null with the Treat Empty as Null setting:

 

 

But this maps the element to NULL instead of omitting it:

 

“field3”: NULL,

 

I can think of a Script approach that might get you to what you’re after, but it’s imperfect:

 


<arc:set attr="output.data">
<arc:call op="messageRead" out="data"> data.data | regexreplace('<\x^\/<>\]+>\l\s\]*<\/\;^<>\]+>', '') | regexreplace('<\x^\/<>\]+>\l\s\]*<\/\;^<>\]+>', '')]</arc:call>
</arc:set>
<arc:push item="output" />

 

Note that this works because the Regex pattern:

 

<>^\/<>]+>^\s]*<\/]^<>]+>

 

Matches a single opening tag, 0 or more whitespace characters, and one single closing tag - but note that for your example, it makes two passes at the data - one that removes the subfield3 and subfield4 elements, and a second pass to remove the empty subnode2, so you’d need to repeat the regex for every degree of depth of empty subtrees that you wanted to remove. 


Hello James,

we are building a structure that was globally defined by a group of companies and this definition states that empty nodes should be removed from the JSON structure.

This is not so much because the final destination could not handle empty nodes, but when it comes to exchanging data in high number, means millions of messages, minimizing the size of a message is an important issue. Also this global definition needs to be followed precisely, because it will be used by yet unknown recipients.

Anyway, I agree that the script regexreplace could be an option, but as you already stated, it is not very perfect.

We will stick to building a condition for each single node for the moment, hoping that such structures do not come up too often.

Maybe your development can think of some nice setting in the future either in the XMLMap or JSON Connectors.

Thanks
  Gordon