user3824001
user3824001

Reputation: 19

Is there a way to directly convert XML to JSON with the namespaces removed without any intermediate XML to XML transformations?

The output of the following XML:

<users>
    <user>
        <id:name>Shikhar</id:name>
        <id:age>31</id:age>
    </user>
    <user>
        <xd:name>Shashank</xd:name>
        <xd:age>29</xd:age>
    </user>
</users>

should not have the id: or xd: in it.

{
  "user": [
    {
      "name": "Shikhar",
      "age": 31
    },
    {
      "name": "Shashank",
      "age": 29
    }
  ]
}

I know how to convert from XML to XML and then to JSON. But I want a direct conversion.

Upvotes: 1

Views: 789

Answers (1)

Michael Kay
Michael Kay

Reputation: 163352

Off-the-shelf programs to convert XML to JSON almost invariably produce something that isn't quite the JSON you want. That's because you know more about the semantics of the data than the general-purpose program does. Some of the utilities are more customizable than others, but none is perfect.

I think for most real-life conversions you should expect to do some tweaking either of the XML pre-transformation, or of the JSON post-transformation. Tweaking the XML is probably easier because there's nothing quite as powerful as XSLT on the JSON side.

Upvotes: 1

Related Questions