You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: tutorial.md
+32-32
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,17 @@
1
1
# JSONata
2
2
JSON query and transformation language
3
3
4
-
###Introduction
4
+
###Introduction
5
5
The primary purpose of this language is to extract values from JSON documents, with the
6
6
additional capabilities to combine these values using a set of basic functions
7
7
and operators, and also the ability to format the output into any arbitrary JSON structure.
8
8
9
-
###Basic Selection
9
+
###Basic Selection
10
10
To support the extraction of values from a JSON structure, a location path syntax is defined.
11
11
In common with XPath, this will select all possible values in the document that match the
12
12
specified location path. The two structural constructs of JSON are objects and arrays.
13
13
14
-
####Navigating JSON Objects
14
+
####Navigating JSON Objects
15
15
A JSON object is an associative array (a.k.a map or hash).
16
16
The location path syntax to navigate into an arbitrarily deeply nested structure of
17
17
JSON objects comprises the field names separated by dot '.' delimiters.
@@ -84,7 +84,7 @@ The following expressions yield the following results when applied to this JSON
84
84
85
85
86
86
87
-
####Navigating JSON Arrays
87
+
####Navigating JSON Arrays
88
88
JSON arrays are used when an ordered collection of values is required.
89
89
Each value in the array is associated with an index (position) rather than a name, so in order to address
90
90
individual values in an array, extra syntax is required to specify the index.
@@ -113,7 +113,7 @@ array will be queried for selection.
113
113
| `Phone.number[0]`| `[ "0203 544 1234", "01962 001234", "01962 001235", "077 7700 1234" ]`| Might expect it to just return the first number, <br>but it returns the first number of each of the items selected by `Phone`
114
114
| `(Phone.number)[0]`| `"0203 544 1234"`| Applies the index to the array returned by `Phone.number`. One use of [parentheses](#parenthesized-expressions-and-blocks).
115
115
116
-
#####Top level arrays, nested arrays and array flattening
116
+
#####Top level arrays, nested arrays and array flattening
117
117
Consider the JSON document:
118
118
```
119
119
[
@@ -133,24 +133,24 @@ the document as follows:
133
133
| `$[0].ref[0]` | `1` | returns element on first position of the internal array
134
134
| `$.ref` | `[ 1, 2, 3, 4 ]` | Despite the structure of the nested array, the resultant selection<br>is flattened into a single flat array. The original nested structure<br>of the input arrays is lost. See [Array constructors](#array-constructors) for how to<br>maintain the original structure in the results.
135
135
136
-
###Complex selection
136
+
###Complex selection
137
137
138
-
####Wildcards
138
+
####Wildcards
139
139
Use of `*` instead of field name to select all fields in an object
140
140
141
141
Expression | Output | Comments|
142
142
| ---------- | ------ |----|
143
143
| `Address.*` | `[ "Hursley Park", "Winchester", "SO21 2JN" ]` | Select the values of all the fields of `Address`
144
144
| `*.Postcode` | `"SO21 2JN"` | Select the `Postcode` value of any child object
145
145
146
-
####Navigate arbitrary depths
146
+
####Navigate arbitrary depths
147
147
Descendant wildcard `**` instead of `*` will traverse all descendants (multi-level wildcard).
148
148
149
149
Expression | Output | Comments|
150
150
| ---------- | ------ |----|
151
151
| `**.Postcode` | `[ "SO21 2JN", "E1 6RF" ]` | Select all `Postcode` values, regardless of how deeply nested they are in the structure
152
152
153
-
####Predicates
153
+
####Predicates
154
154
At any step in a location path, the selected items can be filtered using a predicate - [expr]
155
155
where expr evaluates to a Boolean value. Each item in the selection is tested against
156
156
the expression, if it evaluates to true, then the item is kept; if false, it is removed
| `[Address, Other.'Alternative.Address'].City` | `[ "Winchester", "London" ]` | Selects the `City` value of both <br>`Address` and `Alternative.Address` objects
305
305
306
306
307
-
####Object constructors
307
+
####Object constructors
308
308
In a similar manner to the way arrays can be constructed, JSON objects can also be constructed in the output.
309
309
At any point in a location path where a field reference is expected, a pair of braces `{}` containing key/value
310
310
pairs separated by commas, with each key and value separated by a colon: `{key1: value2, key2:value2}`. The
| `Phone{type: number}` | `{ "home": "0203 544 1234", "office": "01962 001235", "mobile": "077 7700 1234" }` | One of the `office` numbers was lost because it had a duplicate key
0 commit comments