News for hackers. Stuff that matters.
Re:Specifying JSON by ythl on Thursday May 19, 2016 @10:27AM (#11730447)
Even if my data looks like a document, I refuse to use XML. It's just too clumsy to traverse compared to JSON.
Re:Specifying JSON by nly on Thursday May 19, 2016 @10:41AM (#11730565)
Devs who think JSON is wonderful to work with probably think so because they're using a dynamically typed language.

The advantage of a schema is you can validate your data and move to static typing in only a few lines of code. It's for this reason that I think the best option these days is to just use something like protobufs v3, which has a stable JSON serialization and a sensible schema specification.

But even for objecty data, rather than "documents", XML doesn't have to be clumsy. Checkout the code samples here for C++

http://www.codesynthesis.com/products/xsd/

Re:Specifying JSON by jimbokun on Thursday May 19, 2016 @10:36AM (#11730520)
XPath, XSLT and XQuery are pretty awesome, in their special way.

Although stringly-typed XPath has the same problems as SQL, building complex queries through string concatenation. This is where JSON shines, because you usually just convert to a tree of your language's native data structures in one line and use your languages native facilities to traverse it from there.

Re:Specifying JSON by andyjohnson0 on Thursday May 19, 2016 @11:21AM (#11730888)
Is there an equivalent of xpath for searching/traversing in-memory js object graphs?
Re:Specifying JSON by kisstheblade on Thursday May 19, 2016 @01:17PM (#11731786)
Re:Specifying JSON by Chyzwar on Thursday May 19, 2016 @11:35AM (#11730977)
you can use chained map/reduce.

object.listOfItems .reduce((item) => item.type === 'car') .map((item) => item.name));

This is way more powerful than xpath.

Re:Specifying JSON by AlbertoGP on Thursday May 19, 2016 @02:41PM (#11732579)
Hi, I think that instead of "reduce((item) => item.type === 'car')" you actually meant "filter((item) => item.type === 'car')".
Re:Specifying JSON by arethuza on Thursday May 19, 2016 @12:47PM (#11731533)
What about things like axes in XPath?
Re:Specifying JSON by mhd on Thursday May 19, 2016 @11:57AM (#11731160)
Sometimes all you need is printf, not direct memory access to the character byte array.
Re:Specifying JSON by dschep on Thursday May 19, 2016 @02:16PM (#11732351)
There's JMESPath: http://jmespath.org/
Re:Specifying JSON by MichaelGG on Thursday May 19, 2016 @11:33AM (#11730968)
My biggest problem with using JSON, apart from the needless quotation marks on identifiers, is the lack of comments. Any human-editable format (like using .json for project config files or build files) should have comments.
Re:Specifying JSON by tracker1 on Thursday May 19, 2016 @01:00PM (#11731647)
I'm in favor of using YAML [0] or INI [1] formats when a file is meant to be edited by a user. YAML is close enough to JSON, but does support comments, and the structure is a bit cleaner for user editing.

[0] https://en.wikipedia.org/wiki/YAML [1] https://en.wikipedia.org/wiki/INI_file

Re:Specifying JSON by TMSZ on Friday May 20, 2016 @01:37PM (#11739809)
There are few ways of inserting comments: http://stackoverflow.com/questions/244777/can-i-use-comments...

If you do not care too much about exchanging data with other apps then parser/generator supporting javascript style comments (e.g. json-cpp) would work. With each JSON value it can store "commentBefore" and "commentAfter". If you need you can strip these comments any time by parse + write cycle with "collectComments" option inactive.

Re:Specifying JSON by MichaelGG on Friday May 20, 2016 @04:58PM (#11741471)
Adding data and calling it a comment isn't really a comment.

And I find the whole "run minifiy/strip first" to be a hilarious suggestion from Douglas Crockford. It can be rephrased as "If you want comments in JSON, for a config file say, then don't use JSON."

That, and XML requiring the tag name in the closing tag, are some of the obvious, silly, mistakes. XML in particular. Without the named closing tags, just using, say, <>, would reduce the apparent bloat and annoyance by a very large factor.

Re:Specifying JSON by drdaeman on Thursday May 19, 2016 @11:22AM (#11730898)
XML has its bad sides (hell, I won't ever forget hacking Perl's SOAP::Lite to make it be able to talk with some extremely enterprisey Java service), but its core isn't as bad as many try to picture it.

The really good thing about XML, that it - unlike JSON - is extensible.

And traversing both JSON and XML (without the weird parts) are no-brainers. Every (or about so) mainstream language has libraries that make either format really transparent to use.

Re:Specifying JSON by gnaritas on Sunday May 22, 2016 @12:41PM (#11749238)
> Every (or about so) mainstream language has libraries that make either format really transparent to use.

That's really only true about XML, that is not the case with JSON. JSON is a pain to work with in C# for example compared to XML.

Re:Specifying JSON by drdaeman on Thursday May 26, 2016 @07:56AM (#11777215)
I have different experience.

I did some toy project in C# and haven't found handling JSON any painful. Maybe I just haven't hit the bad cases, though.

I don't exactly remember what I've used and what I wrote, but I think I had just installed some library (IIRC it was Json.NET), put some annotations on classes, and got my serialization. For deserialization I didn't wanted to write throwaway classes so I just made a JSONPath query and iterated over the result objects, looking at necessary properties. Nothing particularly different from how things are in, say, Python, except maybe for some extra type checking.

Re:Specifying JSON by catshirt on Thursday May 19, 2016 @10:32AM (#11730478)
really? xpath is one of the more redeeming values of XML. i'm not sure i can call it clumsy.

i refuse to use XML just like everyone else, but not because it's hard to parse data from.

Re:Specifying JSON by rileymat2 on Thursday May 19, 2016 @11:41AM (#11731038)
One of the nicer experiences I had was using xpath to write some basic automated tests on a xhtml strict page. Everything just worked really well.
Re:Specifying JSON by gnaritas on Sunday May 22, 2016 @12:39PM (#11749227)
In statically typed languages, JSON is awful and clumsy compared to XML which has a standard parser and query language. There's pretty much no case where JSON is easier to traverse than XML. I'd challenge you to back up that statement.