JSON schema for XML – Code Utility

I am trying to get the JSON structure for this specific xml structure
XML:

<categories>
  <category id="12">Deals</category>
  <category id="15">Navigation</category>
  <category id="16">Personalization</category>
</categories>

If I try JSON structure like this

{
  "categories": {
  {
   "category": "Products and Services",
   "id": "13"
   },
   {
    "category": "Customer Service",
    "id": "913",
    }
  }
}

I get array of categories like this.

<?xml version="1.0" encoding="UTF-8" ?>
<root>
  <categories>
    <category>Products and Services</category>
    <id>13</id>
  </categories>
  <categories>
    <category>Products and Services</category>
    <id>913</id>
  </categories>
</root>

I am trying to get one categories element with arrays for category. I do not want to lose ID nor the value held by category element.

Could you please suggest me how do I get JSON for the given XML without losing the attribute within the element, element value and also without creating multiple categories? Thanks in advance

There’s no definitive mapping from XML to JSON or vice-versa: there isn’t a single right answer to your question. Nor have you really explained what you are trying to achieve.

The most natural way of representing the same information in JSON is probably like this:

[
  { "id":12, "value":"Deals" },
  { "id":15, "value":"Navigation" },
  { "id":16, "value":"Personalization" }
] 

and this reflects that fact that in XML, “objects” have to be named but “values” don’t, whereas in JSON, “objects” aren’t normally named but values always are.

So you can immediately see that no tool is going to be able to automatically convert from the most natural XML representation of the data to the most natural JSON representation, or vice versa, because each representation contains something that is missing in the other.

Read more here: Source link