# json.jsonpath

> A module of json_mlake's API.

## Structs

### `JSONPath`

```mojo
struct JSONPath
```

A compiled RFC 9535 query.

Compiling separates the work that depends only on the query from
the work that depends on the document, so a query used against many
documents pays for parsing, validation and regular expression
compilation once.

Implements: `Movable`

Fields:

- `segments: List[_Segment]`
- `selectors: List[_Selector]`
- `queries: List[_QueryRef]`
- `exprs: List[_Expr]`
- `cmps: List[_Slot]`
- `funcs: List[_Func]`
- `args: List[_Slot]`
- `literals: List[Value]`
- `regexes: List[Regex]`
- `root: Int`

#### `__init__`

```mojo
def __init__(out self)
```

#### `compile`

```mojo
def compile(path: String) -> Self
```

Parse and validate a query.

Args:

- `path`: The query, which must be a complete
    `jsonpath-query` with no leading or trailing
    whitespace.

Raises: Error: If `path` is not a valid RFC 9535 query. Section 2.1
requires an implementation to reject an invalid query
rather than guess at a meaning for it.

#### `query`

```mojo
def query(self, document: Value) -> List[Value]
```

The values of the nodes this query selects, in order.

#### `paths`

```mojo
def paths(self, document: Value) -> List[String]
```

The normalized paths of the nodes this query selects.

A normalized path (RFC 9535 section 2.7) is itself a query, in
a canonical form that names exactly one node: `$['a'][0]`. It
is not a JSON Pointer, and the two must not be swapped for each
other.

#### `query_with_paths`

```mojo
def query_with_paths(self, document: Value) -> List[Tuple[String, Value]]
```

Each selected node as its normalized path and its value.

## Functions

### `jsonpath_query`

```mojo
def jsonpath_query(document: Value, path: String) -> List[Value]
```

Query a JSON document with an RFC 9535 JSONPath expression.

Example:
var doc = loads('{"users":[{"name":"Alice"},{"name":"Bob"}]}')
var names = jsonpath_query(doc, "$.users[*].name")
Returns `[Value("Alice"), Value("Bob")]`.

Args:

- `document`: The document to query.
- `path`: The query, for example `$.store.book[?@.price < 10].title`.

Raises: Error: If `path` is not a valid query.

### `jsonpath_one`

```mojo
def jsonpath_one(document: Value, path: String) -> Value
```

The first node a query selects.

Args:

- `document`: The document to query.
- `path`: The query.

Raises: Error: If `path` is not a valid query, or selects no node.
