{"title":"Check JSONPath Matches Before Writing the Assertion","excerpt":"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.","canonicalUrl":"https://automationtester.in/blog/tools-reviews/find-jsonpath-matches-before-asserting","category":{"name":"Tools & Reviews","slug":"tools-reviews"},"tags":["jsonpath","api-testing","test-assertions","json"],"author":{"name":"Shashank Rawlani","url":"https://shashank.rawlani.com","linkedin":"https://www.linkedin.com/in/shashankrawlani/"},"datePublished":"2026-10-07T05:30:00.000Z","dateModified":"2026-10-14T10:26:49.503Z","coverImage":{"url":"https://ik.imagekit.io/automationtester/automationtester-in/blog/covers/v3/find-jsonpath-matches-before-asserting.webp","alt":"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."},"contentHtml":"<p>Put a sample response and a JSONPath expression into the <a href=\"https://automationtester.in/tools/jsonpath-tester\">JSONPath Tester</a>. 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.</p>\n\n<p>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.</p>\n\n<h2 id=\"path-paid-order\">Find the paid order ID</h2>\n\n<p>Replace the sample JSON in the left pane with this small response:</p>\n\n<pre class=\"language-json\"><code class=\"language-json\">{\"orders\":[\n  {\"id\":\"A-17\",\"total\":499,\"paid\":true},\n  {\"id\":\"A-18\",\"total\":0,\"paid\":false}\n]}</code></pre>\n<a href=\"https://automationtester.in/tools/jsonpath-tester#p=eyJ2IjoxLCJpbnB1dCI6IntcIm9yZGVyc1wiOltcbiAge1wiaWRcIjpcIkEtMTdcIixcInRvdGFsXCI6NDk5LFwicGFpZFwiOnRydWV9LFxuICB7XCJpZFwiOlwiQS0xOFwiLFwidG90YWxcIjowLFwicGFpZFwiOmZhbHNlfVxuXX0iLCJleHByIjoiJC5vcmRlcnNbPyhALnBhaWQgPT0gdHJ1ZSldLmlkIn0\">Try this in JSONPath Tester</a>\n\n<p>Enter the complete expression <code>$.orders[?(@.paid == true)].id</code>. The tool reports <code>1 match</code> and shows <code>[\"A-17\"]</code> in the result pane. The selection is conditional on <code>paid</code>; <code>$.orders[0].id</code> would happen to return the same value for this response but would silently change meaning if the array order changed.</p>\n\n<p>Now set the first order's <code>paid</code> value to <code>false</code>. The result becomes zero matches. This second check matters: a path that always returns the first ID can look correct on one friendly fixture.</p>\n\n<h2 id=\"path-syntax-boundary\">Know the supported syntax</h2>\n\n<p>This tool supports child names, array indexes and wildcards, slices, recursive descent, and simple filters such as <code>@.paid == true</code> or <code>@.total &gt; 0</code>. It is a small browser evaluator, not a promise that every expression in RFC 9535 works here. For example, adding <code>&amp;&amp; @.total &gt; 0</code> to the filter above produces <code>Unsupported filter literal</code> rather than a combined condition.</p>\n\n<p>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.</p>\n\n<h2 id=\"path-assertion\">Turn a query into a useful assertion</h2>\n\n<p>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 <code>A-17</code>. A later extra paid order should then force a decision about whether the test expects one order or many. <a href=\"https://automationtester.in/tools/json-diff\">JSON Diff</a> can help locate a changed field when a saved fixture stops matching.</p>\n\n<p><strong>Why do I see an array for one match?</strong> A JSONPath query selects a set of nodes. The tool displays their values as an array, even when the count is one.</p>\n\n<p><strong>Does no match mean the expression is wrong?</strong> 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.</p>\n\n<h2 id=\"path-apply\">Check the assertion against two cases</h2>\n\n<p>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.</p>\n\n<h2 id=\"path-references\">JSONPath syntax and tool reference</h2>\n<ul>\n<li><a href=\"https://www.rfc-editor.org/info/rfc9535/\">RFC 9535, JSONPath</a>: the standard query model for selecting JSON values.</li>\n<li><a href=\"https://automationtester.in/tools/jsonpath-tester\">AutomationTester.in JSONPath Tester</a>: the browser input, expression, match count, and result pane.</li>\n</ul>\n"}