10 ms·
JSONSelect
- cubicle67 15y agoI think I'm missing something here, because I can achieve the same using normal js stuff = { "author": { "name": { "first": "Lloyd", "last": "Hilaiel" }, "drinkPref": [ "whiskey", "beer", "wine" ], }, "thing": "JSONSelect site", "license": "(cc) BY-SA" } stuff.author.drinkPref[0] > "whiskey"
- bitanarch 15y agoBut selecting a list of similar items in plain JavaScript would be troublesome.
- kalleboo 15y agoTheir 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)
- sambeau 15y agoI'd love to see a javascript+json version of xquery. It would rock as a templating language. Naming it might be tricky though.
- riffraff 15y agothere is a minimal spec, and implementations, of JSONPath, which may be enough for you
- alinajaf 15y agoCouldn't this be done with a simple map function? Not sure the exact javascript but in ruby it would be as simple as: favourite_drinks = authors.map { |a| a[:favourite_drink] } 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).
- Groxx 15y agoIt 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.
- subbu 15y agoUnderscore.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.
- Groxx 15y agoIt'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. [1]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map https://developer.mozilla.org/en/JavaScript/Reference/Global...
- aquark 15y agoBut what happens in the case where there is no author element? 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.
- lloydhilaiel 15y agowith the reference implementation: 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.
- arethuza 15y agoThat made me wonder if there is something that uses an XPath like approach rather than CSS selectors, turns out there is just such a thing: http://goessner.net/articles/JsonPath/ http://goessner.net/articles/JsonPath/ These tools could be fun for writing map functions for CouchDB.
- audionerd 15y agoYou might want to check out RQL, which builds on JsonPath: http://www.sitepen.com/blog/2010/11/02/resource-query-language-a-query-language-for-the-web-nosql/
- anarchitect 15y agoI've been using jLinq - http://www.hugoware.net/Projects/jLinq http://www.hugoware.net/Projects/jLinq
- mhansen 15y agoPlease someone make this into a commandline utility that lets you pipe and query JSON around. My toolkit is missing a JSON slicer and splicer.
- lloydhilaiel 15y agoI 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 :)
- arethuza 15y agoI thought "that would be interesting to build, I could call it jsawk" - turns out it exists already: https://github.com/micha/jsawk https://github.com/micha/jsawk
- haberman 15y agohttps://github.com/benbernard/RecordStream https://github.com/benbernard/RecordStream
- perlpimp 15y agoFrankly it feels alot like LDAP.
- GlennS 15y agoMy gut reaction is that 'feels like CSS' is not a selling point.
- zdw 15y agoMy prediction is that JSON will eventually reach feature parity with XML (this is quite similar to XPath), then we'll dump it for YAML or something. Great.
- wvenable 15y agoJSON 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.
- arethuza 15y agoI'd argue that Lisp s-expressions are simpler.
- sambeau 15y agoS-expressions can contain code. Json expressions are data only.
- maxtilford 15y agoThat S-expressions can/often contain code doesn't make them more complicated when used for data only. Anything can contain code.
- Ixiaus 15y agoIt 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...
- prodigal_erik 15y agoYou 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.
- nerd_in_rage 15y agoCSS is the worst. And this seems unnecessarily verbose: nth-child(1)
- Groxx 15y agoI'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 :/
- audionerd 15y agoIt 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 – http://www.sitepen.com/blog/2010/11/02/resource-query-language-a-query-language-for-the-web-nosql/ 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: http://mbostock.github.com/d3/#selections ... and jLinq http://www.hugoware.net/Projects/jLinq
- blago 15y agoAs far as selectors go, it looks a lot better than JSONPath. I wonder how do they compare in speed.