4 ms·
Well, XML is complicated, so it's hard to build support for, and it's verbose, so it's heavy on the wire. Frankly, I think JSON is a better format in most conte
by qwertyuiop924 10y ago
Well, XML is complicated, so it's hard to build support for, and it's verbose, so it's heavy on the wire. Frankly, I think JSON is a better format in most contexts.
- barrkel 10y agoThe biggest problem with XML is that it's a node labeled tree that makes the schema choice between leaf node and attribute for scalar data almost arbitrary, whereas JSON is an edge labeled tree without the same choice. Most programming languages use edge labeled graphs for in memory data structures, so the semantic distance is lower with JSON.
- beagle3 10y agoIndeed. Furthermore, JSON readily differentiates between a single element {a: 'hello'} and a vector with one element {b: ['hello']}, as do most programming languages. XML does not, which leads to weird constructs like <Names><Name>a</Name></Names> to indicate that more than one name is possible. (Except .. if you actually use a schema with your XML parser, that indicates more than one is possible. But almost no one does). JSON also differentiates numbers from strings, etc. As a result, in my experience, JSON tends to be more robust in real world use - even when a schema is available.
- marktangotango 10y agoGreat explanation, I'd never heard it put that way before, thanks!
- qwertyuiop924 10y agoIndeed. Even translating to Lisp, which has closer datastructures than most languages, the XML is translated to an edge-labeled tree.
- nialo 10y agoCan you explain what you mean by JSON being an edge labeled tree in more detail? I don't understand and would really like to.
- zachrose 10y agoTaking a stab at this... Let's say we have a dog who has four paws. In XML: <dog> <paw health="ok"> <paw health="ok"> <paw health="ok"> <paw health="ok"> </dog> In JSON: { "paws": [ { "health": "ok" }, { "health": "ok" }, { "health": "ok" }, { "health": "ok" }, ] } I think what the GP is getting at is that JSON is always describing the relationships between a thing and another thing, rarely the things themselves. In the JSON version, for example, it can be assumed that an object in the "paws" array is a paw. This example is sort of a straw man. The JSON version could be wrapped with { "dog": {...} } and the individual XML paws could be wrapped in a <paws> element. But in any case, JSON doesn't need you to give an explicitly label the type of each paw, just what they belong to and what's known about them.
- qwertyuiop924 10y agoI'll step up to the plate to give a more technical answer. JSON is an edge labelled tree, XML is node labelled tree. Let's see what that means, but first, let's talk about what nodes, edges, trees, and labels. You may already know, but I don't want to make no assumptions. First, a tree: A tree is a datastructure with nodes, which reference other nodes, and each node is only referred to by one other node. Now, XML is obviously a tree, with each tag being a node: <dog> /| |\ / | | \ / | | \ <paw> | | <paw> / \ / \ / \ <paw> <paw> However JSON is also a tree: however, instead of tags, we have arrays and objects: Well, actually, I'm not going to draw that. I'm typing on a phone, and it was hard enough making that last one. So, you know, just imagine it. And if you imagine hard enough, you just might notice that this graph is edge labelled, rather than node labelled. A node, as you may recall, is just a thing on the tree, like a tag, or an object or an array. Queue the music! TO THE TUNE OF "NOUNS" FROM SCHOOLHOUSE ROCK: Oh any list through which you can go (like a array, a linked list, or an arraylist), And any structure that you can show (like a hashmap, or a struct), If they have pointers you can follow (from an object in a tree), You know they're nodes, you know they're nodes Aaaanyway, an edge is the link between two nodes, and labels are just names. JSON labels edges: "I want the first value in the array you got at key "foo" from the root object." XML labels nodes: "I want the paragraph tag with the id of 'foo' inside the body tag inside the html tag." You see, with the JSON, the nodes themselves didn't have labels, just the links between them: With XML, it was the opposite: There was no name for the links, instead there were names for the objects. GGP reminds us that most programming languages do it the same way JSON does (when was the last time referred to the Foo object in the Bar object in the head Baz object when coding?), and so JSON maps better to the kind of datastructures we use most of the time.