XML To JSON Questions

Having trouble installing Oxygen? Got a bug to report? Post it all here.
Jamil
Posts: 97
Joined: Thu Oct 23, 2008 6:29 am

XML To JSON Questions

Post by Jamil »

I am noticing unexpected behavior with XML to JSON conversions. Here is a sample file I created to illustrate my question:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<root attribute1="" attribute2="">
  <node1 attribute1="">
    <subnode1>TEST</subnode1>
    <subnode2></subnode2>
    <subnode3></subnode3>
  </node1>
</root>
Based on the XML above, this is how I would expect the JSON to appear post conversion:

Code: Select all

{
	"root":
	{
		"attribute1": "",
		"attribute2": "",
		"node1":
		{
			"attribute1": "",
			"subnode1": "TEST",
			"subnode2": "",
			"subnode3": ""
		}
	}
}
This is not happening though. The latest editor converts JSON to this instead:

Code: Select all

{
  "root": {
    "attribute1": "",
    "attribute2": "",
    "node1": {
      "attribute1": "",
      "subnode1": "TEST",
      "subnode2": {},
      "subnode3": {}
    }
  }
}
I searched through all options to not find anything to change this.

Are there any solutions to have the conversion meet expectations?
teo
Posts: 57
Joined: Wed Aug 30, 2017 3:56 pm

Re: XML To JSON Questions

Post by teo »

Hi Jamil,

The JSON after conversion should be the one you mentioned, indeed.
I tested and it really seems a problem, a regression compared to version 22.1.
I will log an issue for further investigation.

Best regards,
Teo
Teodor Timplaru
<oXygen/> XML Editor
http://www.oxygenxml.com
florin_nica
Posts: 32
Joined: Wed Sep 09, 2020 3:17 pm

Re: XML To JSON Questions

Post by florin_nica »

Hi Jamil,

Just a few clarifications related to the XML - JSON conversion. Starting from version 23.0, we made a little change to the conversion of empty XML elements.

Thus, an empty XML element with both start tag and end tag, such as:

Code: Select all

<subnode></subnode>
is converted to an empty JSON Object, i.e.

Code: Select all

"subnode": {}
and an empty self-closing XML element, such as:

Code: Select all

<subnode/>
is converted to an empty JSON String, i.e.

Code: Select all

"subnode": ""
We did that in order to preserve the round trip conversion, which would otherwise have been lost because we did not differentiate between these 2 types of elements.

So, if you want the previous post conversion's results, the solution is to use empty self-closing XML elements. There isn't currently an option to control this conversion.

Regards,
Florin
Jamil
Posts: 97
Joined: Thu Oct 23, 2008 6:29 am

Re: XML To JSON Questions

Post by Jamil »

florin_nica wrote: Mon Jan 10, 2022 1:15 pm We did that in order to preserve the round trip conversion, which would otherwise have been lost because we did not differentiate between these 2 types of elements.

So, if you want the previous post conversion's results, the solution is to use empty self-closing XML elements. There isn't currently an option to control this conversion.

I am not fully understanding the need for this change, but there is no difference between

Code: Select all

<subnode></subnode>
and

Code: Select all

<subnode/>
I think an option to control the desired output would have been a better approach instead of requiring the source XML to have one or the other.
florin_nica
Posts: 32
Joined: Wed Sep 09, 2020 3:17 pm

Re: XML To JSON Questions

Post by florin_nica »

Hi Jamil,

I added an issue on our issue tracker to investigate the possibility of adding such an option. I will let you know as soon as possible when (or if) this feature will be implemented.

Thank you for your interest and feedback!

Regards,
Florin
florin_nica
Posts: 32
Joined: Wed Sep 09, 2020 3:17 pm

Re: XML To JSON Questions

Post by florin_nica »

Hi Jamil,

I'm coming back to you with some good news. We added an option to control the desired output when converting XML to JSON, as you suggested. The new option is available in the new version of Oxygen 24.1 released yesterday.

Best regards,
Florin
Post Reply