JSONPath Array Examples: First, Last, Filter, Length and Nested Arrays
Most real JSON is a list of things: orders, users, products, log entries. So most JSONPath questions are really array questions. How do I get the first item? The last one? Every name? Only the items where the status is "paid"? How many items are there? This guide answers each of these with an expression you can copy, and shows the exact result.
The example document
Every example below runs against this JSON. Paste it into the JSONPath Tester to try each expression live.
{
"orders": [
{ "id": 101, "customer": "Ann", "status": "paid", "total": 25.5, "items": ["book", "pen"] },
{ "id": 102, "customer": "Ben", "status": "pending", "total": 99, "items": ["lamp"] },
{ "id": 103, "customer": "Cara", "status": "paid", "total": 140, "items": ["chair", "desk", "lamp"], "coupon": "WELCOME" },
{ "id": 104, "customer": "Dan", "status": "refunded","total": 12, "items": [] }
]
}1. Get one item by index
$.orders[0] β the first order (id 101)
$.orders[0].customer β "Ann"
$.orders[2].total β 140Indexes start at 0, so [0] is the first item and [2] is the third. An index that does not exist, like [10], returns no result instead of an error.
2. Get the last item
$.orders[-1] β the last order (id 104)
$.orders[-1].customer β "Dan"
$.orders[-2].customer β "Cara" (second to last)Negative indexes count from the end. This is the easiest way to read the newest entry of a log or the latest record of an API response.
3. Get several items
$.orders[0,2].customer β "Ann", "Cara"
$.orders[0:2].customer β "Ann", "Ben" (index 0 and 1)
$.orders[1:].customer β "Ben", "Cara", "Dan" (all except the first)
$.orders[:2].id β 101, 102 (the first two)
$.orders[::2].id β 101, 103 (every second item)A comma picks exact positions. A slice [start:end:step] picks a range. Like in Python, the end index is not included, so [0:2] returns two items, not three.
4. Get one field from every item
$.orders[*].customer β "Ann", "Ben", "Cara", "Dan"
$.orders[*].total β 25.5, 99, 140, 12[*] means "every item". It turns an array of objects into a simple list of one field, which is what you usually want for a chart, a test or a spreadsheet column.
5. Filter items by a value
Filters keep only the items that match a condition. Inside [?( )], the @ sign means "the current item".
$.orders[?(@.status == 'paid')].id β 101, 103
$.orders[?(@.status != 'paid')].id β 102, 104
$.orders[?(@.total > 50)].customer β "Ben", "Cara"
$.orders[?(@.total >= 25.5 && @.total <= 99)].id β 101, 102
$.orders[?(@.status == 'pending' || @.status == 'refunded')].id β 102, 104Use ==, !=, <, <=, > and >= to compare, and && (and) or || (or) to combine conditions. Put text values in quotes; numbers without quotes.
@.total == '99' (with quotes) finds nothing, because the value is the number 99, not the text "99". If a filter returns nothing, check the type first.6. Filter by "has this field"
$.orders[?(@.coupon)].customer β "Cara" (only orders with a coupon)
$.orders[?(!@.coupon)].customer β "Ann", "Ben", "Dan"A filter with just a path checks that the field exists (and is not null or false). Add ! in front to get the items without it. This is handy for finding incomplete records.
7. Filter by text pattern
$.orders[?(@.customer =~ /^a/i)].id β 101 (name starts with "a", any case)
$.orders[?(@.status =~ /pa/)].id β 101, 103=~ tests a regular expression. The i at the end means "ignore upper and lower case". Regex support differs between libraries (RFC 9535 uses the functions match() and search() instead), so test the expression in the tool you will use.
8. Count the items (array length)
$.orders.length β 4
$.orders[2].items.length β 3
$.orders[?(@.items.length > 1)].id β 101, 103
$.orders[?(@.items.length == 0)].id β 104 (orders with no items).length is not part of the official standard, but many tools support it, including the JSONPath Tester, jsonpath-plus and Jayway (Java, where it is written .length()). In RFC 9535 the same thing is written as a function: $.orders[?length(@.items) > 1].
9. Arrays inside arrays
$.orders[*].items[*] β "book", "pen", "lamp", "chair", "desk", "lamp"
$.orders[*].items[0] β "book", "lamp", "chair" (first item of each order)
$.orders[-1].items[*] β (nothing: the last order has no items)Chain [*] to go one level deeper. The result is a flat list, so $.orders[*].items[*] gives you every item of every order in one list.
10. Find an array anywhere
$..items[*] β every item, no matter how deep "items" is
$..[?(@.total > 100)].customer β "Cara".. (recursive descent) searches at every depth. Use it when you don't know the exact structure, then replace it with a direct path once you do, because direct paths are faster on large files.
$.orders[2].total; change [2] to [*] or a filter and you're done.Cheat sheet: JSONPath arrays
| I want⦠| Expression |
|---|---|
| The first item | $.list[0] |
| The last item | $.list[-1] |
| The first N items | $.list[:3] |
| All except the first | $.list[1:] |
| One field from every item | $.list[*].name |
| Items where a field equals a value | $.list[?(@.status == 'paid')] |
| Items with a number above X | $.list[?(@.total > 50)] |
| Items that have a field | $.list[?(@.coupon)] |
| Items that do not have a field | $.list[?(!@.coupon)] |
| The number of items | $.list.length |
| Items inside nested arrays | $.list[*].items[*] |
Common problems
- No results: check the spelling and case of each key (
Ordersis notorders), and whether a value is a number or a string. - Filter on the wrong level:
$.orders.statusreturns nothing, becausestatusis inside each item. Use$.orders[*].status. - Works in one tool, not another: JSONPath libraries differ in details such as regex,
.lengthand functions. Start with the simplest form.
New to JSONPath? Start with the JSONPath tutorial. To use these expressions in code, see JSONPath in JavaScript, Python and Java.