Re:JSONSelect
by kalleboo on Saturday May 21, 2011 @07:01AM
(#2570659)
Their site has a pretty bad example. Instead imagine it's an array of authors instead. Then their selector would give you a list of all their favourite drinks, which would require a loop in normal JS.
I think I'd prefer something more XPath-like than CSS myself (as someone linked in another comment)
Internally I'd imagine the language will have to loop through the json anyway. Are there any other benefits I'm missing (apart from of course the nice css-like syntax, which is perfectly valid).
Re:JSONSelect
by Groxx on Saturday May 21, 2011 @12:37PM
(#2571110)
It could be, yes. `map` isn't built-in on all browsers, though, which I think has hurt its use. i.e., most people don't think about it, so it doesn't get used as often.
Re:JSONSelect
by subbu on Saturday May 21, 2011 @01:33PM
(#2571222)
Underscore.js and Prototype.js both define this function. You can include this and use map. Underscore delegates it to ECMAScript 5's native map if available.
Re:JSONSelect
by Groxx on Saturday May 21, 2011 @05:40PM
(#2571809)
It's definitely 'out there', and you can always just copy-paste to get the same code and delegation[1], but the lack of default availability is a pretty damaging blow. It shouldn't be, but the Javascript world isn't exactly a pantheon of high-quality code.
Re:JSONSelect
by wvenable on Saturday May 21, 2011 @11:44AM
(#2571026)
JSON is almost the simplest untyped text-based tree serialization format that could possibly be designed. In terms of complexity, XML and YAML are closer together.
Re:JSONSelect
by Ixiaus on Saturday May 21, 2011 @06:38PM
(#2571940)
It isn't that S-expressions can contain code, it's that they are so simple that based on a very simple syntactical rule set certain tokens can be interpreted as code or data and because of that you can easily "tokenize" pieces of "code" to make it "data" and vice versa (that's a really flexible feature). Kind of notable that S-expressions are an integral part of the language and not just a "data format" of the language...
You could easily invent a programming language expressed in JSON literals, it just wouldn't be quite the same as javascript. I'd argue that sexprs are simpler because they only use one container type (alists and plists are merely conventions, not baked into the grammar). However, JSON's omission of the symbol/string distinction is one argument the other way.
Re:JSONSelect
by pnathan on Sunday May 22, 2011 @12:02AM
(#2572577)
That might be a slightly amusing syntactical overlay on Common Lisp. Hash-tables are a bit manky to deal with directly IMO; setting up a reader macro in JSON-esque syntax might be quite usable.
Re:JSONSelect
by Ixiaus on Saturday May 21, 2011 @10:31PM
(#2572439)
Yes, you can, probably not too different in nature to Racket's syntactical sugar with using "[" (square brackets) to make reading the LISP code easier... And yes, your argument for their simplicity is actually what I was trying to express (much less successfully than you did).
Re:JSONSelect
by Ixiaus on Saturday May 21, 2011 @06:31PM
(#2571932)
I doubt it ever will. The reason why I use JSON is because it's a very simple data format; when I need more expressive and structured data interchange I always fallback on XML+XSD+XSLT - something JSON will probably never catch up with (because that already exists and why make JSON needlessly complex in order to handle what that kind of stack does?).
I think web developers get caught up in bashing XML a lot for no good reason - just because it isn't directly applicable to a simple RESTful API doesn't mean it isn't incredibly useful, it's meant for machines anyway!
XMLs great promise was that it was human readable.
Lo and behold, it is more readable by non-bit-addled humans than whatever binary format is used in COBRA RPC.
But no, XML wasn't meant for machines... its just that people today programatically generate and consume XML far more often than they do it by hand.
Re:JSONSelect
by audionerd on Saturday May 21, 2011 @04:32PM
(#2571682)
It would be great to see a standard emerge here. I've watched with interest as JSONPath and JSONQuery proposals have been presented, JS ports of Linq, and this latest proposal: RQL –
There's an Arel-style JS implementation to build RQL strings using chained methods, which makes it quite conducive to composition and makes it feel more like functional programming.
Re:JSONSelect
by Groxx on Saturday May 21, 2011 @05:43PM
(#2571815)
I'd imagine it wouldn't be hard to tweak the library to use "nc(1)" instead.
For "real" CSS in particular, I prefer a bit of verbosity over awk-like code. And you should likely be using a CSS generator anyway, it doesn't even have variables, for Knuth's sake :/
I hope to build jsonselect support on top of yajl and expose it as a native command line tool, so fast stream based filtering... but that's still ideaware at this moment :)
I think I'd prefer something more XPath-like than CSS myself (as someone linked in another comment)
Naming it might be tricky though.
[1]: https://developer.mozilla.org/en/JavaScript/Reference/Global...
With normal javascript you would get an exception .. hopefully with this you just get back undefined without having to check every step in the path.
JSONSelect.match(".nonexistantkey .somechild", obj) returns []
Alternately, if you use the JSONSelect.forEach() method your callback will just not be invoked.
So no existence testing before access for recursive values. A main benefit of this thing in javascript.
http://goessner.net/articles/JsonPath/
These tools could be fun for writing map functions for CouchDB.
Great.
I think web developers get caught up in bashing XML a lot for no good reason - just because it isn't directly applicable to a simple RESTful API doesn't mean it isn't incredibly useful, it's meant for machines anyway!
But no, XML wasn't meant for machines... its just that people today programatically generate and consume XML far more often than they do it by hand.
http://www.sitepen.com/blog/2010/11/02/resource-query-langua...
There's an Arel-style JS implementation to build RQL strings using chained methods, which makes it quite conducive to composition and makes it feel more like functional programming.
Along those lines are D3's selections:
... and jLinqFor "real" CSS in particular, I prefer a bit of verbosity over awk-like code. And you should likely be using a CSS generator anyway, it doesn't even have variables, for Knuth's sake :/
https://github.com/micha/jsawk