How to treat empty/null XML node as empty string in JSON, while using XML to JSON policy?

Hello all,

I’m trying to use the XMLtoJSON policy where I’m facing difficulty converting an empty XML node to empty string or as JSON null

Input XML:

<Root>
	<Code>A</Code>
	<Desc/>
	<Exm>A8SAI38</Exm>
</Root>

I’m using the XMLtoJSON policy with below options:

<Options>
	<RecognizeNumber>true</RecognizeNumber>
	<RecognizeBoolean>true</RecognizeBoolean>
	<RecognizeNull>true</RecognizeNull>
	<NullValue>NULL</NullValue>
	<TextNodeName>TEXT</TextNodeName>
	<StripLevels>1</StripLevels>
</Options>

Actual:

{
  "Code": "A",
  "Desc": "NULL",
  "Exm": "A8SAI38"
}

Expected:

{
  "Code": "A",
  "Desc": null,
  "Exm": "A8SAI38"
}

OR

{
  "Code": "A",
  "Desc": "",
  "Exm": "A8SAI38"
}

How can I achieve this?

@dchiesa1 @sidd-harth @API-Evangelist

The answer has been mentioned here
https://www.googlecloudcommunity.com/gc/Apigee/XML-to-JSON-policy-Default-behavior-of-NullValue/td-p/61569

The documentaion here (https://docs.apigee.com/api-platform/reference/policies/xml-json-policy#nullvalue)) is incorrect.

Removing the below config did the trick

<NullValue>NULL</NullValue>
1 Like

Thanks for the note about the documentation. I agree, it’s wrong or misleading. I’ll get it fixed.

1 Like