Skip to Content
Technical Articles
Author's profile photo Juan Vasquez

How to solve array issue from xml to Json (Rest receiver)

When you use Rest receiver, it’s common to use array structures.

In some cases the array has only one position. In this case the transformation has an issue. The destination structure don’t recognize the array.

The trivial solution for this is to add dummy element to the array. you need to design this with the receiver syste team. They must to know the last  position is dummy, you can use aspecial value, like id=-1, or position = 999. Values that are not valid from business perspective.

Let me show a little example

You can see element PositionArray, the last iteration has a position 9999

You can see discounts with value= 9999

 

Then after transformation you can see:

“PositionArray”: [  –> open array

“Discounts”: [ –> Open array

 

<?xml version="1.0" encoding="UTF-8"?>
<ns2:Structure xmlns:ns2="urn:acme.com:externalservices">
	<Invoice>226437</Invoice>
	<Client>1025738</Client>
	<PositionArray>
		<Position>4</Position>
		<Discounts>
			<Descuento>BC</Descuento>
			<Value>2926000.0</Value>
		</Discounts>
		<Discounts>
			<Descuento>DC</Descuento>
			<Value>-380380.0</Value>
		</Discounts>
		<Discounts>
			<Descuento>ST</Descuento>
			<Value>483667.0</Value>
		</Discounts>
		<Discounts>
			<Descuento>W2</Descuento>
			<Value>63641.0</Value>
		</Discounts>
		<Discounts>
			<Descuento>WT</Descuento>
			<Value>10182.0</Value>
		</Discounts>
		<Discounts>
			<Descuento>9999</Descuento>
			<Value>9999</Value>
		</Discounts>
	</PositionArray>
	<PositionArray>
		<Position>9999</Position>
		<Discounts>
			<Descuento>ZCBC</Descuento>
			<Value>9999</Value>
		</Discounts>
		<Discounts>
			<Descuento>ZCBC</Descuento>
			<Value>9999</Value>
		</Discounts>
	</PositionArray>
</ns2:FacturaDTO>

 

{
  "ns2:Structure": {
    "-xmlns:ns2": "urn:acme.com:externalservices",
    "Invoice": "226437",
    "Client": "1025738",
    "PositionArray": [
      {
        "Position": "4",
        "Discounts": [
          {
            "Descuento": "BC",
            "Value": "2926000.0"
          },
          {
            "Descuento": "DC",
            "Value": "-380380.0"
          },
          {
            "Descuento": "ST",
            "Value": "483667.0"
          },
          {
            "Descuento": "W2",
            "Value": "63641.0"
          },
          {
            "Descuento": "WT",
            "Value": "10182.0"
          },
          {
            "Descuento": "9999",
            "Value": "9999"
          }
        ]
      },
      {
        "Position": "9999",
        "Discounts": [
          {
            "Descuento": "ZCBC",
            "Value": "9999"
          },
          {
            "Descuento": "ZCBC",
            "Value": "9999"
          }
        ]
      }
    ]
  }
}

 

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Varinder singh
      Varinder singh

      Hi Juan,

       

      SAP already updated REST adapter sometime back to handle this problem. With the enhanced xml/json conversion, array fields can be declared directly in channel. More details can be found here: https://blogs.sap.com/2016/01/13/rest-adapter-in-sap-pipo-enhanced-xmljson-conversion/

       

      Regards,

      Varinder