6 ms·
JSON Path: $.store.book[?@.price < 10].title Python: [x['title'] for x in data['store']['book'] if x['price'] < 10] Javascript: data.store.book.filte
by Silphendio 2y ago
JSON Path:
$.store.book[?@.price < 10].title
Python:
[x['title'] for x in data['store']['book'] if x['price'] < 10]
Javascript:
data.store.book.filter(x=>x.price < 10).map(x=>x.title)
- fmbb 2y agoThat javascript version is obviously objectively the best alternative.
- tgv 2y agoIt's also the one that's least optimizable.
- Zenzero 2y agoIf you're that focused on optimization you wouldn't be traversing JSON. As an aside the js example above could be simplified to a reduce().
- OskarS 2y agoYeah, it's not bad. Guido famously preferred the list comprehension style over the functional style, but when you have nested data types and the "monadic style" (ish) functions, it does really make sense. You could imagine it in Python (lets pretend lists have map and filter): data['store']['book'].filter(lambda x: x.price < 10).map(lambda x: x.title) Yeah, not nearly as good. The syntax sugar of a.b instead of a['b'] and arrow functions instead of lambda really does make a pretty big difference.
- vbezhenar 2y agoBest syntax for closures I've ever seen in Scala. Would look something like data.store.book.filter(_.price < 10).map(_.title) Every language should just adapt it.
- lylejantzi3rd 2y agoI will never understand why people find the need to cram as much "mystery meat" code into one line as humanly possible. It makes it much harder to understand, debug, and optimize.
- williamcotton 2y agoIt cuts out the programerese and makes it easier to read?
- im3w1l 2y agoThe less clutter there is, the more the remaining elements pop.
- lylejantzi3rd 2y agoCramming a bunch of chained commands on a single line doesn't reduce clutter. In fact, it increases it.
- OskarS 2y agoRaku does a version of that as well, it's sick.
- librasteve 2y agoin Raku, this data.store.book.filter(_.price < 10).map(_.title) would be written as data.store.book.grep(*.price < 10).map(*.title)
- williamcotton 2y agoF# just added this syntax: let possibleNow = people |> List.distinctBy _.Name |> List.groupBy _.Age |> List.map snd |> List.map _.Head.Name |> List.sortBy _.ToString()
- chuckadams 2y ago
- BossingAround 2y agoYou know, I think you're right. I never realized that Python list comprehension is basically filter/map (and reduce is an external function). That is actually pretty horrible syntax for it from that perspective.
- arethuza 2y agoI actually prefer the JSONPath version, probably I used to like XPath and I'm not hugely fond of JavaScript.
- sbarre 2y agoAll these examples assume an understanding ahead of time of your data structure. Can you write examples in Python and Javascript where you'd extract those titles from an arbitrary JSON structure? ;-)
- Silphendio 2y agoYou mean searching for keys? $..book[?@.price<10].title Yeah, I don't think javascript has that function in the standard library. Writing one is not super complicated, but having to put that into every file (or importing it) is not ideal. find_key = (data, key) => { if(data instanceof Array){ return data.map(x=>find_key(x, key)).flat() } if(data instanceof Object){ let res = Object.keys(data).map(x=>find_key(data[x], key)) if(data.hasOwnProperty(key)){ res.push(data[key]) } return res.flat() } return [] } find_key(data, "book").filter(x=>x.price < 10).map(x=>x.title)
- aeonik 2y agoI tried this once, and I accidentally invented a poorly implemented and incomplete version of Lisp.
- g4zj 2y ago> All these examples assume an understanding ahead of time of your data structure. How would one use JSONPath to extract all book titles from an arbitrary JSON structure? Also, when might that use case apply? I can't think of when I've ever needed to do something like this, but I'm interested in learning. :)
- sbarre 2y agoJSONPath has excellent documentation, you can absolutely answer that question for yourself very easily.
- pydry 2y agoSo do you regularly hard code the number 10 in your code? I think you missed my point. In realistic code you'd be using string interpolation to put the number 10 into this query language, and worrying about injection vulnerabilities while you did it. Or even calling another arbitrary function to do the filtering. Which this query language can't handle at all.
- ruuda 2y agoRCL (https://rcl-lang.org https://rcl-lang.org): [ for book in input.store.book: if book.price < 10: book.title ]
- wmil 2y agoThose become a bit messier if store or book can be null.
- arter 2y agoYep. You wouldnt use json path as replacement to any language. It might be marginally useful in configurations or passing queries between different services. But the complex syntax limits it in both cases, because you cannot easily automatically modify the query. In the case of configs it would be great to analyze hundreds of configs on different systems and change them automaticaly, same with queries exchanged between services which might even get stored in a database. I do not understand why domain languages aren't designed with limited syntax in mind. In the style of lisp for instance. Because actually being able to programatically work with the language is a massive advantage that imo far outweights your own frustration with typing a paranthesis or two extra.
- exceptione 2y agoI think in the Javascript version you have to check for null as well.
- int_19h 2y agoWith https://www.jsoniq.org https://www.jsoniq.org, you can do either: store.book[][$$.price lt 10].title or the more verbose: for $book in store.book[] where $book.price lt 10 return $book.title