Hacker News new | ask | show | jobs
by homedog 4901 days ago
Legitimate question: can someone tell me why something like this would be useful? Don't most libraries that handle json decoding make parsing it extremely easy? I read through the examples on the site, and I'm still not convinced something like JSONPath is necessary for what they're doing.
2 comments

Parsing and accessing are two different tasks. Even if you have an entire JSON (or XML) data structure in memory, getting to the right pieces can be difficult. “Return all of the order entries contained in an a shipment of type ‘Billable‘ whose total price is less than $500.” In the JSON world you’d typically write some imperative code to loop through the data. XPath allows you describe things like this without having to get into the nitty gritty of how the traversal is actually performed, similar to how CSS selectors work with the browser DOM.

I do look forward to the JSON community (re?)developing schema and transformation languages to go along with this.

The project that I am working is document-oriented, meaning that the schema cannot be well controlled. We end up with a lot of code is very guarded code that looks something like this:

  if(((json.author || {}).lastPublish || {}).publishDate)) {
    do something
  }
It can be replaced with:

  if(jsonPath(json, "$.author.lastPublish .publishDate")) {
    do something
  }
Or better, get all book publish dates, regardless of number of books, if any even exist, and those that might have a missing publishDate:

  publishDates = jsonPath(json,"$.author.books[*].publishDate");