I am designing an API for my webapp.
I was thinking to support only JSON responses (not XML) because more streamlined.
But I have just bumped to this XML:
Opening caveat: In this answer I am deliberately standing back from the original question, and expanding the scope somewhat. Sometimes to ask the question behind the question we need to look at the big picture first.
Just as all animals are equal, all translations from JSON to XML, and from XML to JSON are unequal. There can be no mathematically correct answers to this question, and even if there are, they may be wrong. All answers will be a matter of opinion, and depend on the exact use-case(s).
JSON and XML both support and omit concepts that make perfect, reversible and native translations difficult.
To name just some:
XML requires a root element. JSON does not.
XML distinguishes between Elements and Attributes. Attributes are leaves, Elements maybe leaves or branches. JSON objects are close to Elements, but have no direct equivalent to Attributes.
JSON has arrays; the nearest XML equivalent is a parent Element, but some translations fall foul where there is only one child element.
XML has namespaces….. The less said about those the better…
JSON is intended to be compact and lightweight, and is closer (in my humble opinion) to YAML than XML.
Where a concept exists in the source language, but not in the target language, we need to be inventive and creative in our translation. Your use-cases are key.
e.g.
Should the result be as native as possible in the target language? Would we model it like this in JSON? Would we model it like this in XML?
Should the result be reversible to the original source, even at the expense of “native feel”?
Is compactness a criteria? This can be a case for JSON over an element-heavy XML, but not for JSON translated from XML and designed to be translated back.
To make a comparison with human languages: In Swiss German we love our diminutives: Nouns are routinely reduced to the small form where this would be strange in English; but even more bizarrly, we have diminutives of verbs!
So: “wir machen es köchele”, might translate technically to “we will cook littely”, or “we do a little cooking” but either would be poor and incorrect english, and somehow miss the idea.
“we will do a spot of cooking” or “we will have some fun cooking” would be much closer to the original idea. But I suspect that Anthea Bell (of Asterix-to-english translation fame) would truly get the point; “let’s cook a feast….”
Back to the original question. Python programmers have a concept of pythonesque: = most fitting to the core ethos of python. The answer by user166390 (the accepted answer at the time of this answer) strikes me as the most JSONesque.