5 ms·
XML is a lot less pleasurable to parse from a programming point of view though. With a JSON file it's something like: const config = require('config.json'); c
by benbristow 8y ago
XML is a lot less pleasurable to parse from a programming point of view though. With a JSON file it's something like:
const config = require('config.json');
console.log(config.apiKey);
With XML you often have to use things like XPATH etc. to extract the data which is a lot more effort.
- manigandham 8y agoJavascript isn't the only language though. There are lots of XML and JSON parsers and serializers available to convert to an in-memory object. Most relational databases have great XML support too. I don't think people realize that JSON doesn't actually have a querying system at all, you have to deserialize it to an object to use. There is the coming JsonPath standard but that's not well supported yet and is pretty much the same as XPath.
- benbristow 8y agoI think JSON is just universally more easy to use. Every language I’ve used with it is mainly the same in the way it works. JavaScript, Ruby, Python, Perl, PHP... But you’re not wrong. Yes you have to deserialise it. Hence it being an Object Notation system. While you say JSON is trying to catch up with XML in regards to XPath, JSON is trying to catch up to XML with things like JSON Schema.
- pingec 8y ago> While you say JSON is trying to catch up with XML in regards to XPath, JSON is trying to catch up to XML with things like JSON Schema. Absolutely. I would say XSD is the strongest advantage of XML over JSON right now. XSD still has many things to improve but JSON schema is even further behind.
- gridlockd 8y ago> I don't think people realize that JSON doesn't actually have a querying system at all, you have to deserialize it to an object to use. You don't have to deserialize it to an object, you just do that because it's convenient in Javascript (and other dynamic language). It's a feature. Now try using XML like an object, what do you get? A DOM. Which is fine if you wanted a DOM, but I don't want a DOM. I don't need XPath or any of that stuff. These are tools to deal with the complexity of XML, which I don't have, because I am using JSON.
- int_19h 8y agoA DOM is just a tree (and deserialized XML is not always W3C DOM; indeed, in most modern languages, it usually isn't). Deserialized JSON is also just a tree. And even if you deserialize JSON by eval'ing it from JS, it is still deserialization - you're just happening to be reusing the JS parser for that purpose. But any parser is fundamentally a deserializer from the language syntax to an AST. So you're really saying that JSON deserializes to something that is a more natural fit for the language that you're using. And it's true in many cases; but also not so much in others, like when you're dealing with 64-bit integers, or dates, or all those other things that JSON doesn't spec because "complexity". In practice, it just means a proliferation of incompatible ways to represent these things, and utterly insane deserialization behavior in corner cases when implementations try to be "smart" to transparently compensate for JSON lacking something (e.g. https://github.com/JamesNK/Newtonsoft.Json/issues/862 https://github.com/JamesNK/Newtonsoft.Json/issues/862). Conversely, if you are writing in a language that has integral support for XML - say, XQuery, or even VB.NET (https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/xml/accessing-xml https://docs.microsoft.com/en-us/dotnet/visual-basic/program...), the complexity is mostly not there. At the very least, if you control the format - which you have to, if you're in a position to decide what to use - then you can certainly stick to the subset of XML that is not anymore complex than JSON.
- gridlockd 8y agoRemember, we're comparing with XML. There's no dates or numeric types in XML at all. This kind of proliferation is far worse in XML. Sure, there are corner cases and limitations with JSON. I've never experienced that as a significant issue. > Conversely, if you are writing in a language that has integral support for XML - say, XQuery, or even VB.NET... I am not using any of that stuff, nor is there any reason for me to start using it. > At the very least, if you control the format - which you have to, if you're in a position to decide what to use - then you can certainly stick to the subset of XML that is not anymore complex than JSON. ...or I can just use JSON.
- int_19h 8y ago
- emn13 8y agoThat's ridiculous - there are lots of pragmatic xml deserializers available. Not to mention, sometimes you want something like xpath; lack of xpath and lack of validation (schema, whatever) aren't features, they're bugs. The only real advantage json has when it comes to deserialization is that it suggests to a human reader that keys are like object properties, leading to a very obvious deserialization strategy. But even that's a bit misleading, since json allows duplicate keys, just like xml, so a pragmatic deserialization library is going to make that feature impossible. Make no mistake - the culture of straightforward deserialization is hugely valuable! But that's because of its history and other human factors more than the language itself.