# 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="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" },