-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
88 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,75 @@ | ||
# JSONPath Functions | ||
|
||
TODO: | ||
## Standard functions | ||
|
||
## `match()` | ||
These are the standard, built-in function available to JSONPath [filters](./jsonpath-syntax.md#filters). You can also create your own [function extensions](#function-extensions). | ||
|
||
TODO: | ||
### `count()` | ||
|
||
```typescript | ||
count(nodes: JSONPathNodeList): number | ||
``` | ||
|
||
Return the number of nodes in a node list. Usually, `count()` will be given a [filter query](./jsonpath-syntax.md#filter-queries) as its argument, and a call to `count()` must be part of a comparison expression. | ||
|
||
```text title="Example query" | ||
$.users[?count(@.*) > 2] | ||
``` | ||
|
||
### `length()` | ||
|
||
```typescript | ||
length(value: JSONValue): number | undefined | ||
``` | ||
|
||
Return the length of a string or array, or the number of items in an object. Usually, `length()` will be given a [filter query](./jsonpath-syntax.md#filter-queries) as its argument, and a call to `length()` must be part of a comparison expression. | ||
|
||
```text title="Example query" | ||
$.users[?length(@) > 2] | ||
``` | ||
|
||
### `match()` | ||
|
||
```typescript | ||
match(value: string, pattern: string): boolean | ||
``` | ||
|
||
Return `true` if _value_ is a full match to the regular expression _pattern_, or `false` otherwise. | ||
|
||
```text title="Example query" | ||
$.users[?match(@.name, '[Ss].*')] | ||
``` | ||
|
||
### `search()` | ||
|
||
```typescript | ||
search(value: string, pattern: string): boolean | ||
``` | ||
|
||
Return `true` if _value_ contains _pattern_, or `false` otherwise. | ||
|
||
```text title="Example query" | ||
$.users[?search(@.name, '[Aa]')] | ||
``` | ||
|
||
### `value()` | ||
|
||
```typescript | ||
value(nodes: JSONPathNodeList): JSONValue | undefined | ||
``` | ||
|
||
Return the value associated with the first node in _nodes_, if _nodes_ has exactly one [`JSONPathNode`](../api/classes/jsonpath.JSONPathNode.md). Usually, `value()` will be called with a [filter query](./jsonpath-syntax.md#filter-queries) as its argument. | ||
|
||
:::info | ||
[Filter queries](./jsonpath-syntax.md#filter-queries) that can result in at most one node are known as "singular queries", and all singular queries will be implicitly replaced with their value as required, without the use of `value()`. `value()` is useful when you need the value from a query that can, theoretically, return multiple nodes. | ||
::: | ||
|
||
## Well-typedness | ||
|
||
The JSONPath specification defines a [type system for function expressions](https://datatracker.ietf.org/doc/html/draft-ietf-jsonpath-base-20#name-type-system-for-function-ex), and rules for how those types can be used within an expression. JSON P3 will throw a [JSONPathTypeError](../api/classes/jsonpath.JSONPathTypeError.md) as query compile time if it contains expressions that are not deemed to be well-typed. | ||
|
||
Please see [Section 2.4.3](https://datatracker.ietf.org/doc/html/draft-ietf-jsonpath-base-20#name-well-typedness-of-function-) _Well-Typedness of Function Expressions_. | ||
|
||
## `search()` | ||
## Function extensions | ||
|
||
TODO: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,6 +141,19 @@ Selectively include child nodes from the current selection using filters of the | |
- an existence test on the result of a JSONPath query, | ||
- or the truthiness of some function calls, depending on the function's return type. | ||
|
||
```text | ||
$.users[[email protected]] | ||
``` | ||
|
||
```json title="Example output" | ||
[ | ||
{ "name": "John", "score": 86, "admin": true }, | ||
{ "name": "Sally", "score": 84, "admin": false } | ||
] | ||
``` | ||
|
||
### Filter queries | ||
|
||
Inside a filter expression, `@` is the _current node identifier_, starting a new JSONPath query with the current node at the root. | ||
|
||
```text | ||
|
@@ -177,7 +190,7 @@ $.users[[email protected] > 85 && @.score < 100].name | |
["John"] | ||
``` | ||
|
||
### Functions | ||
### Filter functions | ||
|
||
Filter expressions can include calls to predefined functions. For example, the [`match()`](./jsonpath-functions.md#match) function matches nodes against a regular expression, if that node is a string value. | ||
|
||
|
@@ -215,7 +228,7 @@ $.users[0, [email protected] == 'Sally', 'foo'].score | |
|
||
### Recursive descent | ||
|
||
The recursive descent segment (`..[<selectors>]`) selects all nodes, recursively, beneath the current selection in the JSON document tree. There mst be at least one selector. In this example we use shorthand notation for a property name (`score`), but a bracketed selector list is OK too. | ||
The recursive descent segment (`..[<selectors>]`) visits all nodes, recursively, beneath the current selection in the JSON document tree. There mst be at least one selector. In this example we use shorthand notation for a property name (`score`), but a bracketed selector list is OK too. | ||
|
||
```text | ||
$..score | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters