You just got back a 300-line API response. Somewhere inside three levels of nesting is the email field you actually need. So you write a loop, then another loop, then a conditional — and now you're maintaining brittle traversal code that breaks every time the API schema shifts.
There's a better way: JSONPath.
JSONPath is a query language for JSON, similar to how XPath works for XML. Instead of writing code to traverse a structure, you write a short expression that reads like a path. It works across languages, and once you learn the syntax, you'll reach for it constantly.
The Basics: What JSONPath Looks Like
Here's a typical API response — a list of orders, each with nested customer and item data:
{ "store" : { "orders" : [ { "id" : 1 , "customer" : { "name" : "Alice" , "email" : "[email protected]" }, "items" : [{ "sku" : "A1" , "qty" : 2 }, { "sku" : "B3" , "qty" : 1 }] }, { "id" : 2 , "customer" : { "name" : "Bob" , "email" : "[email protected]" }, "items" : [{ "sku" : "C7" , "qty" : 5 }] } ] } }
Enter…