0

I have an XML file which has the following format -

<root>
   <tag>
   This is the first Value.
   </tag>
   <tag>
   This is the second Value.
   </tag>
</root>

When I convert this XML string to JSON in Python using the following code

jsonString=json.dumps(XML_String)

The tag becomes an array and the JSON file I get is like this -

{"root":["tag":"This is the first Value",
         "tag":"This is the second Value"]}

What I actually want is the <root> tag should be an object instead of an array. Like this -

{"root":{"tag":"This is the first Value",
         "tag":"This is the second Value"}}

How to achieve such kind of format?

2 Answers 2

3

What you want to generate is invalid JSON.

Well, perhaps not totally invalid, but at least contrary to good practice: RFC 8259 says: "When the names within an object are not unique, the behavior of software that receives such an object is unpredictable. Many implementations report the last name/value pair only. Other implementations report an error or fail to parse the object, and some implementations report all of the name/value pairs, including duplicates."

So don't do it.

Sign up to request clarification or add additional context in comments.

Comments

0

I solved this problem by creating customized JSON string. And it actually worked. Arrays were not created.

Comments

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.