JSONPath: The XPath We Needed for JSON
4 min read

JSONPath: The XPath We Needed for JSON

793 words

I’ve seen how certain standards and tools become indispensable when working with data. And if there’s one thing we’ve learned over these years, it’s that JSON is everywhere: APIs, logs, configurations, NoSQL databases… The question is no longer whether you’ll work with JSON, but when you’ll face that 15-level nested structure that makes you sigh.

The Problem We’ve All Lived Through

How many times have you had to write something like this?

// The typical code we've all written
const book = data.store.book[0];
const price = book ? book.price : null;
const author = book && book.authors ? book.authors[0] : null;

Or worse yet, those nested loops to filter arrays within objects within arrays. It’s tedious, error-prone, and, frankly, not very elegant.

JSONPath: The Solution We Were Waiting For

Recently I came across this excellent article on PostPilot that explains JSONPath very clearly. And the truth is, it reminded me of something fascinating: JSONPath just became an official standard with the publication of RFC 9535 in 2024.

The curious thing? It’s been 17 years since Stefan Gössner published his original blog post about JSONPath in 2007. Seventeen years for a brilliant idea to become a standard. This reminds me why I always say that “for every minute you dedicate to planning and study, you’ll need 2 minutes less of development”.

What is JSONPath Really?

JSONPath is like XPath but for JSON. It allows you to make declarative queries on complex JSON structures using a simple and expressive syntax.

Instead of writing imperative code, you simply describe what data you want:

// Instead of complex loops
const titles = jp.query(data, '$.store.book[*].title');
// ["The Catcher in the Rye", "To Kill a Mockingbird", "Sapiens"]

// Filters with conditions
const expensiveBooks = jp.query(data, '$..book[?(@.price > 10)]');

// Recursive searches
const allExistingPrices = jp.query(data, '$..price');

Practical Syntax (The Stuff You Really Use)

After years working with different technologies, I’ve learned that what matters isn’t memorizing the entire syntax, but understanding the most useful patterns:

The Essential Basics:

  • $ - The root node (like / in XPath)
  • $.store.book - Property navigation
  • $.store.book[0] - Array element access
  • $.store.book[*] - All elements of an array
  • $..price - Recursive search (all price at any level)

The Filters You Actually Use:

  • $..book[?(@.price > 10)] - Filter by condition
  • $..book[?(@.isbn)] - Filter by property existence
  • $..book[?(@.category=='fiction')] - Filter by exact value

The Useful Functions (RFC 9535):

  • length() - Size of arrays, objects, or strings
  • match() - Complete regular expressions
  • search() - Search for substring
  • count() - Count matching elements

Tools That Will Make Your Life Easier

One of the things I like most about JSONPath is the ecosystem of tools that have emerged around it. Here are some I recommend:

Online playgrounds:

Implementations by language:

  • JavaScript: jsonpath (npm)
  • Python: jsonpath-ng
  • Java: com.jayway.jsonpath
  • C#: JsonPath.Net
  • Go: Various RFC 9535 implementations like jsonpath
  • PHP: jsonpath-php with RFC 9535 support

My Practical Experience

In recent projects where I’ve worked with complex APIs and structured data, JSONPath has saved me hours of development. It’s especially useful when:

  1. Processing API responses where you need to extract specific data
  2. Working with configurations that change frequently
  3. Analyzing logs in JSON format where you’re looking for specific patterns
  4. Transforming data before storing or sending to other systems

The Future is Here

What I find most interesting is that now, with RFC 9535, we finally have an official standard. This means implementations will converge toward a common syntax, which is excellent news for interoperability.

As I always say: “There’s no such thing as a good solution/technology for everything,” but JSONPath is definitely a tool that should be in your arsenal if you work with JSON regularly.

Resources to Go Deeper

Personal Conclusion

JSONPath is one of those tools that, once you incorporate into your workflow, you wonder how you lived without it. It’s elegant, powerful, and now that it’s an official standard, a safe bet for the future.

As always, the best way to learn is by practicing. I recommend trying some of the online playgrounds with your own JSON data and seeing how JSONPath can simplify your code.

Have you used JSONPath in your projects before? What use cases have you found most useful? I’d love to hear about your experience.


Did you find this article useful? Share it with other developers who could benefit from JSONPath. And if you have any questions or experiences to share, don’t hesitate to contact me.

Comments

Latest Posts

4 min

740 words

In my experience with mobile development, I’ve seen how apps become increasingly complex and projects grow uncontrollably. I remember perfectly that feeling of having thousands of lines of code and not being sure what was really being used and what wasn’t.

That’s why I was so struck by the tool that Sentry (formerly from Emerge Tools) just released as open source: Reaper. An SDK that does something that sounds simple but is tremendously useful: find dead code in your mobile applications.

5 min

905 words

Throughout my career, I’ve seen many things change. I’ve gone from Borland to Visual Studio, from vi to Sublime Text, from Sublime to VS Code… And believe me, each change was a deliberate decision that cost me weeks of adaptation. But what’s happening now with AI tools is something completely different.

I’ve found myself using Copilot in the morning, trying Cursor in the afternoon, and checking out Claude Code before going to bed. And I’m not alone. Developers have gone from being faithful as dogs to our tools to being… well, promiscuous.

3 min

548 words

When working on large projects, it’s common to have test suites that can take several minutes to run. And when one of those tests fails early in the execution, it’s frustrating to wait for all the others to complete just to see the full results.

Jest includes a feature I’ve found very useful in development: the bail option, which allows stopping test execution after a certain number of failures. It’s one of those features that once you know and start using, you don’t understand how you lived without it.