Programming Search⌘K

Standardadvanced

JSONPath

Also called RFC 9535, JSON Path.

JSONPath (RFC 9535) is a query language for JSON. A query selects zero or more values from a document with names, indexes, slices, wildcards, descendant segments and filters.

What it is

JSONPath is a query language for JSON. RFC 9535 says that it "defines a string syntax for selecting and extracting JSON (RFC 8259) values from within a given JSON value"1. The query $.store.book[*].author means: from the root, take the member store, then the member book, then every element of that array, then the member author of each element.

The result of a query is a nodelist, a list of nodes. "Nodes are the selectable parts of the query argument"1. A query can select zero, one or many nodes. An empty nodelist is a valid result, so a query that matches nothing is not an error.

Stefan Gössner proposed JSONPath in an article dated 2007-02-21, as an XPath for JSON2. Many libraries implemented the proposal, but they did not agree on the details14. RFC 9535 is "based on Stefan Gössner's popular JSONPath proposal", and it makes a normative specification from the experience with these implementations1.

JSONPath and JSON Pointer do different things. The RFC says that JSONPath "is not intended as a replacement but as a more powerful companion" of JSON Pointer1. A pointer such as /store/book/0 identifies one value3. A JSONPath query can also search for many values.

Syntax or structure

"A JSONPath expression consists of an identifier followed by a series of zero or more segments, each of which contains one or more selectors"1. The identifier $ is the root node. The identifier @ is the current node, and it is valid only inside a filter. The main parts are these1:

Syntax Meaning
$ the root node
[<selectors>] a child segment: it applies the selectors to each input node
..[<selectors>] a descendant segment: it applies the selectors to each input node and to all its descendants
.name, .*, ..name, ..* the short forms of ['name'], [*], ..['name'] and ..[*]
'name' a name selector: the member with that name
* a wildcard selector: all children
3, -1 an index selector: an array element; a negative index counts from the end
0:100:5 an array slice selector: start, end and step
?<logical-expr> a filter selector: the children for which the expression is true
length(@.foo) a function expression in a filter

Bracket notation and dot notation give the same query, so $['store']['book'][0]['title'] and $.store.book[0].title are equal1. One bracket can hold more than one selector, separated by commas, as in $..book[0,1].

A filter can use the comparison operators ==, !=, <, <=, > and >=, the logical operators &&, || and !, parentheses, and function expressions1. A query by itself in a filter is an existence test: $..book[?@.isbn] selects the books that have a member isbn. RFC 9535 defines five functions: length, count, match, search and value. The functions match and search use the regular expression format I-Regexp of RFC 94851.

A query that does not follow the grammar is an error before it runs. "A JSONPath implementation MUST raise an error for any query that is not well-formed and valid"1. A valid query does not cause errors when it runs. An index past the end of an array selects nothing.

Each node in a value has exactly one Normalized Path. A Normalized Path is a query that uses only bracket notation, names in single quotes and non-negative indexes, such as $['book'][3]1. An API can give the result of a query as the values, as the Normalized Paths of the nodes, or as both1.

Examples

The program below is a small evaluator for a subset of RFC 9535. It handles names, wildcards, indexes, slices, descendant segments and simple filters, and it gives each result with its Normalized Path. The document and most queries are the examples of the RFC1. The last line shows the escapes of a member name in a Normalized Path.

import json
import operator
import re

# A subset of RFC 9535: $, .name, ..name, .*, [*], ['name'], [index], [start:end:step],
# and filters of the form [?@.name] and [?@.name<op><number or 'string'>].
TOKEN = re.compile(r"""\.\.|\.|\[\?@\.(\w+)\s*(==|!=|<=|>=|<|>)?\s*(-?\d+(?:\.\d+)?|'[^']*')?\]|\['([^']*)'\]|\[(-?\d*):(-?\d*)(?::(-?\d+))?\]|\[(-?\d+)\]|\[\*\]|\*|\w+""")
ESCAPES = {"\b": "\\b", "\f": "\\f", "\n": "\\n", "\r": "\\r", "\t": "\\t", "'": "\\'", "\\": "\\\\"}
OPS = {"==": operator.eq, "!=": operator.ne, "<": operator.lt, "<=": operator.le, ">": operator.gt, ">=": operator.ge}


def parse(query):
    """Returns a list of (descendant, selector) pairs."""
    if not query.startswith("$"):
        raise ValueError("a query starts with $")
    steps, pos, desc = [], 1, False
    while pos < len(query):
        m = TOKEN.match(query, pos)
        if not m:
            raise ValueError(f"bad query at {pos}: {query[pos:]!r}")
        text, pos = m.group(0), m.end()
        if text in (".", ".."):
            desc = text == ".."
            continue
        steps.append((desc, m))
        desc = False
    return steps


def normal(name):
    """Writes a member name as in a Normalized Path (RFC 9535, section 2.7)."""
    text = "".join(ESCAPES.get(c, f"\\u{ord(c):04x}" if ord(c) < 0x20 else c) for c in name)
    return f"'{text}'"


def kind(x):
    if isinstance(x, str):
        return "string"
    if isinstance(x, (int, float)) and not isinstance(x, bool):
        return "number"
    return "other"


def test(item, name, op, lit):
    """The filter test for one child: an existence test, or a comparison with a literal."""
    if op is None:
        return isinstance(item, dict) and name in item
    want = lit[1:-1] if lit.startswith("'") else float(lit)
    if not isinstance(item, dict) or name not in item:
        return op == "!="  # An empty result is equal only to another empty result.
    got = item[name]
    if kind(got) == kind(want):
        return OPS[op](got, want)
    return op == "!="  # Values of different types are never equal or ordered.


def children(value, path, m):
    """Applies one selector to one node. Yields (path, value) pairs."""
    if isinstance(value, dict):
        items = [(f"{path}[{normal(k)}]", v) for k, v in value.items()]
    elif isinstance(value, list):
        items = [(f"{path}[{i}]", v) for i, v in enumerate(value)]
    else:
        return
    text = m.group(0)
    if text in ("*", "[*]"):
        yield from items
    elif m.group(1):
        yield from ((p, v) for p, v in items if test(v, *m.group(1, 2, 3)))
    elif isinstance(value, list):
        if m.group(8) is not None:
            i = int(m.group(8))
            i = i + len(value) if i < 0 else i
            if 0 <= i < len(value):
                yield items[i]
        elif m.group(5) is not None:
            start, end, step = (int(g) if g else None for g in m.group(5, 6, 7))
            if step != 0:
                yield from (items[i] for i in range(len(value))[slice(start, end, step)])
    else:
        name = m.group(4) if m.group(4) is not None else text
        if name in value:
            yield f"{path}[{normal(name)}]", value[name]


def descendants(path, value):
    """The node, then its descendants, arrays in order."""
    yield path, value
    if isinstance(value, dict):
        for k, v in value.items():
            yield from descendants(f"{path}[{normal(k)}]", v)
    elif isinstance(value, list):
        for i, v in enumerate(value):
            yield from descendants(f"{path}[{i}]", v)


def query(doc, q):
    nodes = [("$", doc)]
    for desc, m in parse(q):
        inputs = [d for p, v in nodes for d in descendants(p, v)] if desc else nodes
        nodes = [c for p, v in inputs for c in children(v, p, m)]
    return nodes


store = json.loads("""{"store": {"book": [
  {"category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95},
  {"category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99},
  {"category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99},
  {"category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99}],
  "bicycle": {"color": "red", "price": 399}}}""")

for q in ["$.store.book[*].author", "$..price", "$..book[-1].title", "$..book[:2].title",
          "$..book[?@.isbn].title", "$..book[?@.price<10].title", "$..book[?@.isbn!='0-553-21311-3'].title",
          "$..book[2].publisher"]:
    print(q)
    for path, value in query(store, q):
        print("   ", path, json.dumps(value))
print(normal("it's"), normal("a\\b"), normal("tab\there"), normal("\x0b"))
Output
$.store.book[*].author
    $['store']['book'][0]['author'] "Nigel Rees"
    $['store']['book'][1]['author'] "Evelyn Waugh"
    $['store']['book'][2]['author'] "Herman Melville"
    $['store']['book'][3]['author'] "J. R. R. Tolkien"
$..price
    $['store']['book'][0]['price'] 8.95
    $['store']['book'][1]['price'] 12.99
    $['store']['book'][2]['price'] 8.99
    $['store']['book'][3]['price'] 22.99
    $['store']['bicycle']['price'] 399
$..book[-1].title
    $['store']['book'][3]['title'] "The Lord of the Rings"
$..book[:2].title
    $['store']['book'][0]['title'] "Sayings of the Century"
    $['store']['book'][1]['title'] "Sword of Honour"
$..book[?@.isbn].title
    $['store']['book'][2]['title'] "Moby Dick"
    $['store']['book'][3]['title'] "The Lord of the Rings"
$..book[?@.price<10].title
    $['store']['book'][0]['title'] "Sayings of the Century"
    $['store']['book'][2]['title'] "Moby Dick"
$..book[?@.isbn!='0-553-21311-3'].title
    $['store']['book'][0]['title'] "Sayings of the Century"
    $['store']['book'][1]['title'] "Sword of Honour"
    $['store']['book'][3]['title'] "The Lord of the Rings"
$..book[2].publisher
'it\'s' 'a\\b' 'tab\there' '\u000b'

The output shows the rules of the RFC:

  • $..price finds the prices of the books and of the bicycle. A descendant segment visits each node before its descendants, and the elements of an array in order1.
  • In the filter ?@.price<10, the operator < compares two numbers or two strings. A string and a number are never less than each other1.
  • In != with @.isbn, a book with no isbn is a match. A comparison with an empty nodelist is == only to another empty nodelist, and != is the negation of ==1.
  • $..book[2].publisher gives an empty nodelist, not an error.

The example files of this page show other parts of the standard:

  • JavaScript. The array slice selector, with the normative steps of the RFC and the examples of section 2.3.4.3.
  • Go. A conversion from a Normalized Path to a JSON Pointer.
  • C#. A walk over all the descendants of a JsonNode tree, with the path strings of JsonNode.GetPath. These strings are not Normalized Paths.
using System.Text.Json.Nodes;

JsonNode doc = JsonNode.Parse("""{"store": {"book": [{"title": "Moby Dick", "price": 8.99}], "a b": {"it's": 1}}}""")!;

// The query $..* in a few lines: every descendant of the root, each node before its descendants.
foreach (JsonNode node in Descendants(doc))
{
    string value = node is JsonValue ? node.ToJsonString() : node.GetType().Name;
    Console.WriteLine($"{node.GetPath(),-26} {value}");
}

static IEnumerable<JsonNode> Descendants(JsonNode node)
{
    IEnumerable<JsonNode?> children = node switch
    {
        JsonObject o => o.Select(member => member.Value),
        JsonArray a => a,
        _ => [],
    };
    foreach (JsonNode? child in children)
    {
        if (child is null) continue;
        yield return child;
        foreach (JsonNode d in Descendants(child)) yield return d;
    }
}
Output
$.store                    JsonObject
$.store.book               JsonArray
$.store.book[0]            JsonObject
$.store.book[0].title      "Moby Dick"
$.store.book[0].price      8.99
$.store['a b']             JsonObject
$.store['a b']['it's']     1
  • Verdict: stdlib. System.Text.Json.Nodes has no JSONPath query function, but the method JsonNode.GetPath returns the path of a node as a string. The walk above visits each node before its descendants, as the descendant segment of RFC 9535 does. The output shows that GetPath is not a Normalized Path: it uses dot notation for simple names, and it does not escape the apostrophe in it's, so the path $.store['a b']['it's'] is not a well-formed JSONPath query. Use the path for messages and logs, and do not parse it again as a query.
JavaScriptGitHub
// The array slice selector of RFC 9535, section 2.3.4.2.2.
function normalize(i, len) {
  return i >= 0 ? i : len + i;
}

function slice(a, start, end, step = 1) {
  const len = a.length;
  if (step === 0) return [];
  start ??= step >= 0 ? 0 : len - 1;
  end ??= step >= 0 ? len : -len - 1;
  const [nStart, nEnd] = [normalize(start, len), normalize(end, len)];
  const out = [];
  if (step > 0) {
    const lower = Math.min(Math.max(nStart, 0), len);
    const upper = Math.min(Math.max(nEnd, 0), len);
    for (let i = lower; i < upper; i += step) out.push(a[i]);
  } else {
    const upper = Math.min(Math.max(nStart, -1), len - 1);
    const lower = Math.min(Math.max(nEnd, -1), len - 1);
    for (let i = upper; lower < i; i += step) out.push(a[i]);
  }
  return out;
}

const a = ["a", "b", "c", "d", "e", "f", "g"];
console.log("$[1:3]    ", slice(a, 1, 3).join(""));
console.log("$[5:]     ", slice(a, 5).join(""));
console.log("$[1:5:2]  ", slice(a, 1, 5, 2).join(""));
console.log("$[5:1:-2] ", slice(a, 5, 1, -2).join(""));
console.log("$[::-1]   ", slice(a, undefined, undefined, -1).join(""));
console.log("$[-2:]    ", slice(a, -2).join(""), "=", a.slice(-2).join(""));
console.log("$[::0]    ", JSON.stringify(slice(a, undefined, undefined, 0)));
Output
$[1:3]     bc
$[5:]      fg
$[1:5:2]   bd
$[5:1:-2]  fd
$[::-1]    gfedcba
$[-2:]     fg = fg
$[::0]     []
  • Verdict: native. JavaScript has no JSONPath, but JSON.parse gives arrays, and the slice selector is a few lines over them. The function follows the normative steps of RFC 9535 for the array slice selector: the default start and end depend on the sign of the step, negative bounds count from the end, and the bounds are clamped to the array. The first five queries and their results are the examples of the RFC. With a positive step, the result is the same as Array.prototype.slice, which inspired the selector. A step of 0 selects nothing.
package main

import (
	"fmt"
	"strconv"
	"strings"
)

// toPointer converts an RFC 9535 Normalized Path, such as $['a'][0], into an RFC 6901 JSON Pointer.
func toPointer(path string) (string, error) {
	if !strings.HasPrefix(path, "$") {
		return "", fmt.Errorf("no root identifier in %q", path)
	}
	var b strings.Builder
	rest := path[1:]
	for rest != "" {
		if !strings.HasPrefix(rest, "[") {
			return "", fmt.Errorf("expected [ at %q", rest)
		}
		rest = rest[1:]
		var token string
		if strings.HasPrefix(rest, "'") {
			name, n, err := unquote(rest)
			if err != nil {
				return "", err
			}
			token, rest = name, rest[n:]
		} else {
			end := strings.IndexByte(rest, ']')
			if end < 0 {
				return "", fmt.Errorf("no ] in %q", rest)
			}
			if n, err := strconv.ParseUint(rest[:end], 10, 64); err != nil || strconv.FormatUint(n, 10) != rest[:end] {
				return "", fmt.Errorf("bad index %q", rest[:end])
			}
			token, rest = rest[:end], rest[end:]
		}
		if !strings.HasPrefix(rest, "]") {
			return "", fmt.Errorf("expected ] at %q", rest)
		}
		rest = rest[1:]
		token = strings.ReplaceAll(strings.ReplaceAll(token, "~", "~0"), "/", "~1")
		b.WriteString("/" + token)
	}
	return b.String(), nil
}

// unquote decodes a 'name' with the escapes of a Normalized Path. It returns the name and the bytes used.
func unquote(s string) (string, int, error) {
	escapes := map[byte]string{'b': "\b", 'f': "\f", 'n': "\n", 'r': "\r", 't': "\t", '\'': "'", '\\': "\\"}
	var b strings.Builder
	for i := 1; i < len(s); i++ {
		switch s[i] {
		case '\'':
			return b.String(), i + 1, nil
		case '\\':
			if i+1 >= len(s) {
				return "", 0, fmt.Errorf("escape at end of %q", s)
			}
			if r, ok := escapes[s[i+1]]; ok {
				b.WriteString(r)
				i++
			} else if s[i+1] == 'u' && i+5 < len(s) {
				code, err := strconv.ParseUint(s[i+2:i+6], 16, 8)
				if err != nil || code >= 0x20 {
					return "", 0, fmt.Errorf("bad escape in %q", s)
				}
				b.WriteByte(byte(code))
				i += 5
			} else {
				return "", 0, fmt.Errorf("bad escape in %q", s)
			}
		default:
			b.WriteByte(s[i])
		}
	}
	return "", 0, fmt.Errorf("no closing quote in %q", s)
}

func main() {
	for _, p := range []string{`$['store']['book'][0]['title']`, `$['a/b']['m~n']`, `$['it\'s']['\u000b']`, `$`, `$[-1]`, `$[01]`, `$.store`} {
		pointer, err := toPointer(p)
		if err != nil {
			fmt.Println("error:", err)
			continue
		}
		fmt.Printf("%-32s %q\n", p, pointer)
	}
}
Output
$['store']['book'][0]['title']   "/store/book/0/title"
$['a/b']['m~n']                  "/a~1b/m~0n"
$['it\'s']['\u000b']             "/it's/\v"
$                                ""
error: bad index "-1"
error: bad index "01"
error: expected [ at ".store"
  • Verdict: stdlib. The Go standard library has no JSONPath, but the package strings and the package strconv are enough to convert a Normalized Path into a JSON Pointer. The conversion needs no JSON value, because a Normalized Path has only names in single quotes and non-negative indexes. The function decodes the escapes of the path, such as \' and \u000b, and then applies the escapes of the pointer, ~0 and ~1. It rejects $[-1] and $[01], which are valid queries but not Normalized Paths, and the dot notation $.store. The reverse conversion needs the value, because the token 0 in a pointer can name an array element or an object member.

Versions

  • 2007. Gössner published the proposal. It used ?(...) for filters and (...) for script expressions that "the underlying script engine" evaluates, for example $..book[?(@.price<10)]2.
  • 2007 to 2024. Libraries in many languages implemented the proposal, each with its own details. JSONPath "delegated parts of a query to the underlying runtime", so "JSONPath expressions became decreasingly portable"1. The JSONPath Comparison project runs the same queries on many implementations and marks each result that "does not match the consensus"4.
  • February 2024. RFC 9535 was published as an IETF Standards Track document1. It registers the media type application/jsonpath for a query1. A filter is now written ?@.price<10 without parentheses, and there are no script expressions.

Pitfalls

  • Many dialects have the same name. An old library, a JSONPath in a tool and RFC 9535 can give different results for the same query4. Before you move a query from one tool to another, check which dialect each tool uses.
  • A filter that the host language evaluates is code. The 2007 proposal handed filter text to the script engine2. The RFC says that this approach "is well known to lead to injection attacks"1. A query that comes from a user must never go to eval:

This code runs but gives a wrong answer on purpose.

from types import SimpleNamespace

books = [{"title": "Sayings of the Century", "price": 8.95}, {"title": "Sword of Honour", "price": 12.99}]


def old_filter(items, expr):
    """The style of the 2007 proposal: the host language evaluates the filter text."""
    return [b["title"] for b in items if eval(expr.replace("@.", "x."), {}, {"x": SimpleNamespace(**b)})]


print(old_filter(books, "@.price < 10"))
# The filter text comes from a request parameter. It is code, and the server runs it.
print(old_filter(books, "@.price < 10 or print('this code ran on the server')"))
Output
['Sayings of the Century']
this code ran on the server
['Sayings of the Century']
  • Queries made from strings. A query built from a variable, such as a member name from a request, can change its meaning. The RFC says that these variables "need to be validated" and "translated (e.g., by escaping string delimiters)"1.
  • Expensive queries. An attacker can send a query that causes "possibly exponential, CPU usage" or, with a recursive descendant segment, a stack overflow. The RFC says that implementations "need to have appropriate resource management"1.
  • Object order is not defined. For a wildcard or a descendant segment, "The order in which the children of an object appear in the resultant nodelist is not stipulated"1. Do not depend on the order of the results from an object. Arrays keep their order.
  • Duplicates stay. "A node may be selected more than once and appears that number of times in the nodelist. Duplicate nodes are not removed"1. Use the Normalized Paths to remove duplicates.
  • Step 0 is not an error. "When step is 0, no elements are selected", which "differs from the behavior of Python, which raises an error in this case"1.
  • null is a value. "JSON null is treated the same as any other JSON value, i.e., it is not taken to mean "undefined" or "missing""1. The existence test ?@.foo is true for a member foo with the value null. To test for null, write ?@.foo == null.
  • Members, not names. A query selects member values. "Member names and members (name/value pairs) cannot be selected"1. To get the names, use the Normalized Paths of the results.

Tools and libraries

  • Other JSON query languages. JMESPath has its own specification. Its expressions start with a name, not with $, and an expression such as foo[*].bar makes what the specification calls "a projection"5. jq has its own filter language. Its function path(path_expression) gives each path as an array of "object keys" and "array indices", and getpath reads the values at such paths6.
  • kubectl. The Kubernetes command-line tool takes JSONPath templates in curly braces, such as {.items[*].metadata.name}. It adds the range and end operators, and it says that "JSONPath regular expressions are not supported"7.
  • SQL. PostgreSQL implements the SQL/JSON path language of the SQL standard, a different language with a similar look. Its filter is written after the path with ? and parentheses, as in the path '$.a[*] ? (@ >= $min && @ <= $max)' in an example of jsonb_path_query8.
  • Rust. The crate serde_json_path runs queries on a serde_json::Value, and it "intends to adhere to the IETF JSONPath standard ( RFC 9535 )"9.
  • .NET. System.Text.Json has no query function. JsonNode.GetPath returns "The JSON Path value" of a node, as a string for messages10.
  • Python, JavaScript, Java and Go. The standard libraries have no JSONPath. Libraries exist for each language. The JSONPath Comparison lists many of them and their results for each query4.

The page JSON Pointer covers the syntax that identifies one value, and JSON Patch and JSON Merge Patch uses pointers to change a document.

References

  1. S. Gössner, G. Normington, C. Bormann (Ed.), RFC 9535: JSONPath: Query Expressions for JSON, February 2024, 1.2 History; 1.4 Overview of JSONPath Expressions; 2.1 Overview; 2.3 Selectors; 2.4 Function Extensions; 2.5.2 Descendant Segment; 2.6 Semantics of null; 2.7 Normalized Paths; 3.1 Registration of Media Type application/jsonpath; 4 Security Considerations; Appendix C JSON Pointer
  2. S. Gössner, JSONPath - XPath for JSON, 2007-02-21, the syntax table and the examples
  3. P. Bryan (Ed.), K. Zyp, M. Nottingham (Ed.), RFC 6901: JavaScript Object Notation (JSON) Pointer, April 2013, 3 Syntax; 4 Evaluation
  4. C. Burgmer, JSONPath Comparison, Explanation; the results table
  5. JMESPath Specification, Grammar; Projections; Filter Expressions
  6. jq manual, path(path_expression); getpath(PATHS)
  7. Kubernetes documentation, JSONPath Support, the functions table; Regular expressions in JSONPath
  8. PostgreSQL 17 documentation, 9.16 JSON Functions and Operators, 9.16.2 The SQL/JSON Path Language; jsonb_path_query
  9. serde_json_path documentation, crate serde_json_path, the crate description; Features
  10. .NET API documentation, JsonNode.GetPath Method, Returns

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source