Skip to main content
Back to Blog

Check JSONPath Matches Before Writing the Assertion

A path that selects the first order can pass while selecting the wrong record. Test a conditional JSONPath against a paid and unpaid order before keeping it in a test.

Shashank Rawlani

Engineering Leader | Builder | Problem Solver | AI Engineer

Published Oct 7, 2026Updated Oct 14, 20262 min read
View AI-ready Markdown
Two abstract order rows move toward a dashed filter. The upper green row reaches one selected value in the right panel. The lower orange row stops at a crossed-out marker inside the filter.

Put a sample response and a JSONPath expression into the JSONPath Tester. It shows the values that match and their count as you edit. Keep the expression only after checking it against both a matching and a non-matching response.

A test extracts the first order ID and passes, even though the order is unpaid. The path selected a position, not the record the test meant to inspect. A filter can express the condition, but it is easy to type one that matches nothing or selects several records.

Find the paid order ID

Replace the sample JSON in the left pane with this small response:

{"orders":[
  {"id":"A-17","total":499,"paid":true},
  {"id":"A-18","total":0,"paid":false}
]}
Try this in JSONPath Tester

Enter the complete expression $.orders[?(@.paid == true)].id. The tool reports 1 match and shows ["A-17"] in the result pane. The selection is conditional on paid; $.orders[0].id would happen to return the same value for this response but would silently change meaning if the array order changed.

Now set the first order's paid value to false. The result becomes zero matches. This second check matters: a path that always returns the first ID can look correct on one friendly fixture.

Know the supported syntax

This tool supports child names, array indexes and wildcards, slices, recursive descent, and simple filters such as @.paid == true or @.total > 0. It is a small browser evaluator, not a promise that every expression in RFC 9535 works here. For example, adding && @.total > 0 to the filter above produces Unsupported filter literal rather than a combined condition.

That error is preferable to a quiet zero-match result. A zero can mean the query is valid but no order qualifies; an error means the expression itself needs work. The tool also reports invalid JSON separately. If your test runner uses a different JSONPath library, run the final expression there too, because supported syntax and result ordering can differ.

Turn a query into a useful assertion

Do not assert only that the returned array is non-empty. For this response, assert that exactly one order matches and that its ID is A-17. A later extra paid order should then force a decision about whether the test expects one order or many. JSON Diff can help locate a changed field when a saved fixture stops matching.

Why do I see an array for one match? A JSONPath query selects a set of nodes. The tool displays their values as an array, even when the count is one.

Does no match mean the expression is wrong? Not by itself. Try a fixture that should match and one that should not. The pair separates a correct negative result from a path aimed at the wrong field.

Check the assertion against two cases

Take one JSONPath assertion from a test. Run it on a positive fixture and a negative fixture, then record the expected count and values. Keep both fixtures with the test so the selection rule remains reviewable.

JSONPath syntax and tool reference