0

I'm using Json2xml module for converting json format to xml format. But, while converting it changes the order of the parameters. How do I convert without changing the order of parameters? Here's my python code.

from json2xml.json2xml import Json2xml

data = Json2xml.fromjsonfile('example.json').data
data_object = Json2xml(data)
xml_output = data_object.json2xml()
print xml_output

example.json

{
    "action": {
      "param1": "aaa",
      "param2": "bbb"
    }
}

The output is

<action>
<param2>bbb</param2>
<param1>aaa</param1>
</action>

Is there a way to convert json to xml without changing the order of parameters?

1 Answer 1

4

Try using an OrderedDict:

from collections import OrderedDict
from json2xml.json2xml import Json2xml

data = Json2xml.fromjsonfile('example.json').data
data = OrderedDict(data)
data_object = Json2xml(data)
xml_output = data_object.json2xml()
print xml_output
Sign up to request clarification or add additional context in comments.

2 Comments

What's the difference? I don't want the items to be sorted. I just want them in the same order as in json.
If you switch the order of the element in your example.json you will see order change reflected in the outputted XML

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.