News for hackers. Stuff that matters.
Re:How to Use JSON Path by pydry on Friday May 03, 2024 @05:49AM (#40245862)
These types of languages are a bad idea, just as XPath was. They are complex enough to be a maintenance/bug risk AND don't bring any additional benefit to just writing code in your normal programming language to do the same thing.

You can take my list comprehensions from my cold, dead hands.

There isn't a use case I've seen where these types of mini languages fit well. Ostensibly, you could give it to a user to write to query JSON in a domain-agnostic way in an app but I think it would just confuse most users as well as not being powerful enough for half of their use cases.

Sometimes it's better just to write code.

Re:How to Use JSON Path by Silphendio on Friday May 03, 2024 @06:24AM (#40246089)
JSON Path:

  $.store.book[[email protected] < 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)
Re:How to Use JSON Path by sbarre on Friday May 03, 2024 @06:56AM (#40246251)
All 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? ;-)

Re:How to Use JSON Path by g4zj on Friday May 03, 2024 @08:30AM (#40246935)
> 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. :)

Re:How to Use JSON Path by sbarre on Friday May 03, 2024 @05:53PM (#40252829)
JSONPath has excellent documentation, you can absolutely answer that question for yourself very easily.
Re:How to Use JSON Path by aeonik on Friday May 03, 2024 @08:14AM (#40246786)
I tried this once, and I accidentally invented a poorly implemented and incomplete version of Lisp.
Re:How to Use JSON Path by Silphendio on Friday May 03, 2024 @07:47AM (#40246565)
You mean searching for keys?

  $..book[[email protected]<10]
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)
Re:How to Use JSON Path by fmbb on Friday May 03, 2024 @06:33AM (#40246140)
That javascript version is obviously objectively the best alternative.
Re:How to Use JSON Path by OskarS on Friday May 03, 2024 @07:03AM (#40246289)
Yeah, 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.
Re:How to Use JSON Path by vbezhenar on Friday May 03, 2024 @07:37AM (#40246496)
Best 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.
Re:How to Use JSON Path by williamcotton on Friday May 03, 2024 @07:48AM (#40246568)
F# just added this syntax:

  let possibleNow = 
      people 
      |> List.distinctBy _.Name
      |> List.groupBy _.Age
      |> List.map snd
      |> List.map _.Head.Name
      |> List.sortBy _.ToString()
Re:How to Use JSON Path by chuckadams on Friday May 03, 2024 @11:27AM (#40248788)
Meanwhile JS is still struggling to define even version 1.0 of a pipe operator, and the proposal has been bikeshedded into shabby oblivion with a syntax that's worse than just pulling out lodash pipe() or similar. TC39 does not fill me with hope.
Re:How to Use JSON Path by OskarS on Friday May 03, 2024 @07:43AM (#40246538)
Raku does a version of that as well, it's sick.
Re:How to Use JSON Path by librasteve on Monday May 06, 2024 @02:44PM (#40278045)
in Raku, this

    data.store.book.filter(_.price < 10).map(_.title)
would be written as

    data.store.book.grep(*.price < 10).map(*.title)
Re:How to Use JSON Path by lylejantzi3rd on Friday May 03, 2024 @08:19AM (#40246830)
I 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.
Re:How to Use JSON Path by williamcotton on Friday May 03, 2024 @08:23AM (#40246865)
It cuts out the programerese and makes it easier to read?
Re:How to Use JSON Path by im3w1l on Saturday May 04, 2024 @05:20AM (#40256147)
The less clutter there is, the more the remaining elements pop.
Re:How to Use JSON Path by lylejantzi3rd on Saturday May 04, 2024 @02:35PM (#40259429)
Cramming a bunch of chained commands on a single line doesn't reduce clutter. In fact, it increases it.
Re:How to Use JSON Path by fwlr on Friday May 03, 2024 @09:34AM (#40247507)
I’m a fan, but let’s go even further. JavaScript has pleasant definitions of functions with

    filter((x) => x.price < 10)
but why can’t we just write

    filter(x.price < 10)
and add a rule to the JS engine that says “when you encounter a ‘syntax error: undeclared identifier x’, rewrite the code to add `(x) => ` in front of where the syntax error occurred, if and only if this rewrite prevents the syntax error”.

You might protest that reacting to syntax errors by inserting extra code and checking if the errors go away is an insane strategy, but I would note that JavaScript is actually a semicolon-terminated language in which most developers never write a semicolon, and the JavaScript engine is already using this insane strategy on nearly every line of modern JS to insert a semicolon whenever it encounters a syntax error, so it’s obviously practical.

Re:How to Use JSON Path by chuckadams on Friday May 03, 2024 @11:23AM (#40248737)
The problem with your approach is that `filter(x.price < 10)` is perfectly valid syntax, it's filter with a single boolean arg. You need something else to trigger the magic: change `x` to `it` and you have Kotlin and Groovy's shorthand syntax -- you just can't define a variable called `it` anymore. If you want closure semantics on arbitrary undefined variables, I think I might have to slap you on general principle ;)
Re:How to Use JSON Path by tubthumper8 on Friday May 03, 2024 @11:15AM (#40248653)
Some languages do this already but with a designated placeholder, like

    filter(_.price < 10)
That may not work because plain underscore is already a valid identifier but another placeholder could potentially be used and no need for the parser backtracking / function insertion (which I don't like the idea of, there may be cases where an undeclared identifier was a bug and it shouldn't be turned into a function)
Re:How to Use JSON Path by lizmat on Saturday May 04, 2024 @05:50AM (#40256270)
That's exactly what the Raku Programming Language does. Except that the "x" is represented by "*", so it reads:

    grep(*.price < 10)
This is referred to as "Whatever-currying": https://docs.raku.org/type/Whatever*
Re:How to Use JSON Path by zzo38computer on Saturday May 04, 2024 @03:00PM (#40259583)
I dislike that syntax, but it might be better to instead use a special syntax that can't be the name of any variable, like the asterisk in Raku as mentioned below. In such a case, perhaps the function that would call it should check if the value is already the correct type and use that instead of calling it as a function, since then it would be possible for the filter condition to be a constant true or false that does not depend on the values being filtered (this is not usually useful, but it is more consistent and sometimes it is useful).

I think that the automatic semicolon insertion is a bad feature of JavaScript.

Re:How to Use JSON Path by exceptione on Friday May 03, 2024 @01:28PM (#40250092)
Kotlin has it instead of _

    val numbers = listOf(20, 19, 7, 12)
    val multiplied = numbers.map { 3 * it }
    // [ 60, 57, 21, 36 ]
Re:How to Use JSON Path by lizmat on Saturday May 04, 2024 @05:46AM (#40256245)
The Raku programming language has, with some tweaks:

    data.store.book.grep(*.price < 10).map(*.title)
Although personally I would write that as:

    data.store.book.map: { .title if .price < 10 }
which combines the filter / map into a single operation.

https://raku.org

Re:How to Use JSON Path by tgv on Friday May 03, 2024 @08:29AM (#40246922)
It's also the one that's least optimizable.
Re:How to Use JSON Path by Zenzero on Friday May 03, 2024 @08:52AM (#40247135)
If 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().

Re:How to Use JSON Path by BossingAround on Friday May 03, 2024 @09:12AM (#40247310)
You 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.
Re:How to Use JSON Path by arethuza on Friday May 03, 2024 @09:18AM (#40247351)
I actually prefer the JSONPath version, probably I used to like XPath and I'm not hugely fond of JavaScript.
Re:How to Use JSON Path by pydry on Friday May 03, 2024 @07:30AM (#40246446)
So 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.

Re:How to Use JSON Path by ruuda on Friday May 03, 2024 @08:42AM (#40247039)
RCL (https://rcl-lang.org):

    [
        for book in input.store.book:
        if book.price < 10:
        book.title
    ]
Re:How to Use JSON Path by wmil on Friday May 03, 2024 @09:05AM (#40247247)
Those become a bit messier if store or book can be null.
Re:How to Use JSON Path by arter on Friday May 03, 2024 @12:44PM (#40249646)
Yep.

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.

Re:How to Use JSON Path by exceptione on Friday May 03, 2024 @01:27PM (#40250083)
I think in the Javascript version you have to check for null as well.
Re:How to Use JSON Path by int_19h on Friday May 03, 2024 @08:02PM (#40253647)
With 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
Re:How to Use JSON Path by Adiqq on Friday May 03, 2024 @06:11AM (#40246006)
> don't bring any additional benefit to just writing code in your normal programming language to do the same thing.

In some cases advantage is that you don't create new code and you just use some relatively standard tool. You just fetch some public package that handles various edge cases and you just prepare script that describes what you want to do with some program. This is useful, if you work in containerized environment and configuration exists as json or yaml. Often I just use jq or yq, instead of reinventing wheel to just read or write some values.

Re:How to Use JSON Path by sesm on Friday May 03, 2024 @07:37AM (#40246494)
One advantage is slightly less typing, which might be useful in CLI tools and UI filters. Also, people don't want to bring a full JS interpreter in those kinds of interfaces for various reasons.
Re:How to Use JSON Path by pydry on Friday May 03, 2024 @07:56AM (#40246647)
Fewer characters to write is an advantage but it's an advantage that is more than offset by the disadvantages of a familiar, more powerful language.

The philosophy of language terseness uber alles died with Perl.

Re:How to Use JSON Path by husam212 on Friday May 03, 2024 @07:27AM (#40246433)
> don't bring any additional benefit to just writing code in your normal programming language

What if the query is implemented in a lower level and more efficient programming language, or probably a completely separate DB engine.

Re:How to Use JSON Path by pydry on Friday May 03, 2024 @07:33AM (#40246458)
What if the filtering criteria required a function call?

The benefits of having a well designed Turing complete language available for the filtering criteria far outweigh the disadvantages.

Re:How to Use JSON Path by chuckadams on Friday May 03, 2024 @11:31AM (#40248835)
The disadvantages in being tied to the execution model of said Turing Complete language are also significant when compared to one that can do optimizations like stream fusion without having to work such optimizations into the language as a whole. But there's no technical reason we can't have both.
Re:How to Use JSON Path by Drakim on Friday May 03, 2024 @06:12AM (#40246013)
I agree, it gives the same vibe as wanting to somehow bring back the simplicity of excel formulas rather than having to write normal code, to revive that dream of convenient one-liners.

But there is a reason excel has a ceiling of maintainability that always turns it into a spaghetti mess once it's big enough.

Re:How to Use JSON Path by stoperaticless on Friday May 03, 2024 @06:47AM (#40246211)
Are you also against file paths?
Re:How to Use JSON Path by psnehanshu on Friday May 03, 2024 @07:22AM (#40246390)
How's that related?
Re:How to Use JSON Path by HarHarVeryFunny on Friday May 03, 2024 @07:51AM (#40246593)
It's pretty much the exact same thing - let's you specify a file to access with a string (pathname) such as "/foo/bar/cat" rather than having to go step by step first open/reading directory "foo", then open/reading directory "bar", then finally accessing file "cat".

With XPath and JSONPath you're just dealing with DOM nodes and children rather than directories and children.

Re:How to Use JSON Path by exyi on Friday May 03, 2024 @08:59AM (#40247198)
Except that XPath and JSONPath are overpowered. I'd welcome a simple standard path syntax for JSON, for instance, I'd want to use for reporting schema errors in JSON document ($.users[10].name must be a string). I can use JSONPath, sure, but even this subset is annoying to parse - compared to a filepath which you can parse with `path.split('/')`.
Re:How to Use JSON Path by HarHarVeryFunny on Friday May 03, 2024 @09:56AM (#40247715)
Agreed - for a lot of use cases all you need is a pathname-like way to refer to elements in the DOM, not a search/filtering mechanism.
Re:How to Use JSON Path by hgyjnbdet on Friday May 03, 2024 @02:37PM (#40250810)
You might like gron then.

https://github.com/tomnomnom/gron

For 90% on my needs this is all I need.

Re:How to Use JSON Path by samatman on Friday May 03, 2024 @03:25PM (#40251316)
One of the advantages of this sort of DSL is that they're easy to share between languages. One needs one implementation of JSON path per programming language, and then it's easy to, for example, iterate on the query in the REPL of a dynamic language, then copy it to a fast compiled language. Or share a query between the browser and the server. That sort of thing.

Regex has similar advantages, if one sticks to the subset of regex which is commonly understood between languages: so less so, for that very reason.

Another plus is the principle of least power. A JSON Path will halt, and it won't make syscalls. There are circumstances where that's useful.

Re:How to Use JSON Path by philstu on Friday May 03, 2024 @04:18PM (#40251921)
If you read the article you'd know that this is about using OpenAPI Overlays and other use cases where JSONPath is literally a requirement. I think you just read the first paragraph then wrote a few about how its bad.

More and more parts of the API ecosystem require JSONPath, and just saying "you should write code instead" doesn't actually help anyone write OpenAPI Overlays, so whats the point?