Live data from Hacker News

Why is JSON so popular? Developers want out of the syntax business

blog.mongolab.com

11–20 of 133 posts

Re: Why is JSON so popular? Developers want out of the syntax business

#11
post #4

The post actually appears to endorse using eval to parse JSON. Not only does that allow invalid JSON through, it disallows some valid JSON, and of course is a huge security hole. If you want to handle JSON data in JavaScript use JSON.parse -- it's the safest, fastest, and most correct path to having your data available to you. [update: edited to remove bizarre use of whole vs hole... boggles ]

Yes! I was shocked to see him recommend the use of a straight eval for parsing JSON. json2.js is a must! JSON.parse and JSON.stringify are your friends. https://github.com/douglascrockford/JSON-js/blob/master/json...

Too my knowledge all browsers now ship with json built in -- if you must work with older browsers (not an entirely unreasonable requirement) you should check for the JSON object first, and if it isn't present load json2.js. Otherwise you're just adding an additional load penalty to your site (and on mobile browsers that can be 100s of milliseconds)

Re: Why is JSON so popular? Developers want out of the syntax business

#12
post #4

The post actually appears to endorse using eval to parse JSON. Not only does that allow invalid JSON through, it disallows some valid JSON, and of course is a huge security hole. If you want to handle JSON data in JavaScript use JSON.parse -- it's the safest, fastest, and most correct path to having your data available to you. [update: edited to remove bizarre use of whole vs hole... boggles ]

Yes! I was shocked to see him recommend the use of a straight eval for parsing JSON. json2.js is a must! JSON.parse and JSON.stringify are your friends. https://github.com/douglascrockford/JSON-js/blob/master/json...

just pointing out to others although the parent probably already knew, but JSON.parse is native in all modern browsers, json2 is only needed if you support older browsers (ie7)

Re: Why is JSON so popular? Developers want out of the syntax business

#14
post #7

Ironically, php provides a way to access XML data almost as easily as JASON data - SimpleXML. Do other languages/frameworks really have nothing similar?

While SimpleXML gives you an easy way to walk the tree, it still requires you to actually do it. All 3 of those examples would require slightly different calls to SimpleXML to handle.

what else do you do with a data structure besides walk it at some point? JSON comes out as an acyclic object graph in javascript, so what it still needs to be walked to actually do something with the data.

The difference between walking JSON and XML is simply the syntax of the parser library your are using.

The problem with XML is it has things that don't map directly to most OO languages, like node order matters, and attributes and elements are similar but different things, and mixed content.

So JSON is nice simply because it's close the the OO style everyone is used to today, and is in fact a subset of one of those languages.

IMO either way we need to settle on a way to interchange structured data, and agree on data types, but I would prefer some binary format, text encoding all this data such a waste of space/time.

Re: Why is JSON so popular? Developers want out of the syntax business

#15

The mostly-just-one-way-to-organize-it approach cannot be overstated here. XPath is neat, but iterating through parser results is still a pain.

That because XML does not map well to Simula style OO.

"Iterating" XML in XSLT (a language designed to manipulate XML) is way less of a pain.

Re: Why is JSON so popular? Developers want out of the syntax business

#16
post #11

Earlier quoted context omitted.

Yes! I was shocked to see him recommend the use of a straight eval for parsing JSON. json2.js is a must! JSON.parse and JSON.stringify are your friends. https://github.com/douglascrockford/JSON-js/blob/master/json...

Too my knowledge all browsers now ship with json built in -- if you must work with older browsers (not an entirely unreasonable requirement) you should check for the JSON object first, and if it isn't present load json2.js. Otherwise you're just adding an additional load penalty to your site (and on mobile browsers that can be 100s of milliseconds)

Json2.js does the check. However, it has to be loaded in order to be checked. Not a big issue if you are minifying and bundling all your js files before sending them to the browser client.

Re: Why is JSON so popular? Developers want out of the syntax business

#18
The baby that has been thrown out with the bathwater here is a schema/data description layer.

NOT, I repeat NOT, I repeat NOT for verification but rather for tools, so that people working in strongly typed languages can interact with JSON services in a reasonable manner.

Unfortunately the one option I see, JSON Schema, appears to have caught the XML/Java bug, and has gotten very complicated.

Re: Why is JSON so popular? Developers want out of the syntax business

#19

Earlier quoted context omitted.

While SimpleXML gives you an easy way to walk the tree, it still requires you to actually do it. All 3 of those examples would require slightly different calls to SimpleXML to handle.

what else do you do with a data structure besides walk it at some point? JSON comes out as an acyclic object graph in javascript, so what it still needs to be walked to actually do something with the data. The difference between walking JSON and XML is simply the syntax of the parser library your are using. The problem with XML is it has things that don't map directly to most OO languages, like node order matters, an…

Reiterating the point the blog post makes, but: It's very very easy to structure the same data in different ways in XML. While JSON has the same issue, it's a lot harder to do and you have less flexibility in making bad storage decisions.

You are absolutely right on walking the structure. JSON just tends to be easier (in my experience), even when working with XML libraries that make it less of a pain.

Post reply on HN