@@ -81,14 +81,12 @@ the data. This method always returns an array of matching values.
81
81
Querying with Expressions
82
82
-------------------------
83
83
84
- The primary way to query the JSON is by passing a JSONPath expression string
85
- to the :method: `Symfony\\ Component\\ JsonPath\\ JsonCrawler::find ` method.
84
+ The primary way to query the JSON is by passing a JSONPath expression string to the :method: `Symfony\\ Component\\ JsonPath\\ JsonCrawler::find ` method.
86
85
87
86
Accessing a Specific Property
88
87
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89
88
90
- Use dot-notation for object keys and square brackets for array indices. The root
91
- of the document is represented by ``$ ``::
89
+ Use dot-notation for object keys and square brackets for array indices. The root of the document is represented by ``$ ``::
92
90
93
91
// Get the title of the first book in the store
94
92
$titles = $crawler->find('$.store.book[0].title');
@@ -157,53 +155,51 @@ the correct escaping of keys and values, preventing syntax errors::
157
155
The :class: `Symfony\\ Component\\ JsonPath\\ JsonPath ` class provides several methods to build your query:
158
156
159
157
* :method: `Symfony\\ Component\\ JsonPath\\ JsonPath::key `
160
- Adds a key selector. The key name will be properly escaped::
158
+ Adds a key selector. The key name will be properly escaped::
161
159
162
- // Creates the path '$["key\"with\"quotes"]'
163
- $path = (new JsonPath())->key('key"with"quotes');
160
+ // Creates the path '$["key\"with\"quotes"]'
161
+ $path = (new JsonPath())->key('key"with"quotes');
164
162
165
163
* :method: `Symfony\\ Component\\ JsonPath\\ JsonPath::deepScan `
166
- Adds the descendant operator ``.. `` to perform a recursive search from the
167
- current point in the path::
164
+ Adds the descendant operator ``.. `` to perform a recursive search from the current point in the path::
168
165
169
- // Get all prices in the store: '$["store"]..["price"]'
170
- $path = (new JsonPath())->key('store')->deepScan()->key('price');
166
+ // Get all prices in the store: '$["store"]..["price"]'
167
+ $path = (new JsonPath())->key('store')->deepScan()->key('price');
171
168
172
169
* :method: `Symfony\\ Component\\ JsonPath\\ JsonPath::all `
173
- Adds the wildcard operator ``[*] `` to select all items in an array or object::
170
+ Adds the wildcard operator ``[*] `` to select all items in an array or object::
174
171
175
- // Creates the path '$["store"]["book"][*]'
176
- $path = (new JsonPath())->key('store')->key('book')->all();
172
+ // Creates the path '$["store"]["book"][*]'
173
+ $path = (new JsonPath())->key('store')->key('book')->all();
177
174
178
175
* :method: `Symfony\\ Component\\ JsonPath\\ JsonPath::index `
179
- Adds an array index selector.
176
+ Adds an array index selector.
180
177
181
178
* :method: `Symfony\\ Component\\ JsonPath\\ JsonPath::first ` / :method: `Symfony\\ Component\\ JsonPath\\ JsonPath::last `
182
- Shortcuts for ``index(0) `` and ``index(-1) `` respectively::
179
+ Shortcuts for ``index(0) `` and ``index(-1) `` respectively::
183
180
184
- // Get the last book: '$["store"]["book"][-1]'
185
- $path = (new JsonPath())->key('store')->key('book')->last();
181
+ // Get the last book: '$["store"]["book"][-1]'
182
+ $path = (new JsonPath())->key('store')->key('book')->last();
186
183
187
184
* :method: `Symfony\\ Component\\ JsonPath\\ JsonPath::slice `
188
- Adds an array slice selector ``[start:end:step] ``::
185
+ Adds an array slice selector ``[start:end:step] ``::
189
186
190
- // Get books from index 1 up to (but not including) index 3
191
- // Creates the path '$["store"]["book"][1:3]'
192
- $path = (new JsonPath())->key('store')->key('book')->slice(1, 3);
187
+ // Get books from index 1 up to (but not including) index 3
188
+ // Creates the path '$["store"]["book"][1:3]'
189
+ $path = (new JsonPath())->key('store')->key('book')->slice(1, 3);
193
190
194
- // Get every second book from the first four books
195
- // Creates the path '$["store"]["book"][0:4:2]'
196
- $path = (new JsonPath())->key('store')->key('book')->slice(0, 4, 2);
191
+ // Get every second book from the first four books
192
+ // Creates the path '$["store"]["book"][0:4:2]'
193
+ $path = (new JsonPath())->key('store')->key('book')->slice(0, 4, 2);
197
194
198
195
* :method: `Symfony\\ Component\\ JsonPath\\ JsonPath::filter `
199
- Adds a filter expression. The expression string is the part that goes inside
200
- the ``?() `` syntax::
196
+ Adds a filter expression. The expression string is the part that goes inside the ``?() `` syntax::
201
197
202
- // Get expensive books: '$["store"]["book"][?(@.price > 20)]'
203
- $path = (new JsonPath())
204
- ->key('store')
205
- ->key('book')
206
- ->filter('@.price > 20');
198
+ // Get expensive books: '$["store"]["book"][?(@.price > 20)]'
199
+ $path = (new JsonPath())
200
+ ->key('store')
201
+ ->key('book')
202
+ ->filter('@.price > 20');
207
203
208
204
Advanced Querying
209
205
-----------------
@@ -237,52 +233,52 @@ The component provides a set of PHPUnit assertions to make testing JSON data mor
237
233
The trait provides the following assertion methods:
238
234
239
235
* :method: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait::assertJsonPathCount `
240
- Asserts that the number of elements found by the JSONPath expression matches an expected count::
236
+ Asserts that the number of elements found by the JSONPath expression matches an expected count::
241
237
242
- $json = '{"a": [1, 2, 3]}';
243
- self::assertJsonPathCount(3, '$.a[*]', $json);
238
+ $json = '{"a": [1, 2, 3]}';
239
+ self::assertJsonPathCount(3, '$.a[*]', $json);
244
240
245
241
* :method: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait::assertJsonPathEquals `
246
- Asserts that the result of a JSONPath expression is equal (`` == ``) to an expected value. This assertion uses type coercion::
242
+ Asserts that the result of a JSONPath expression is equal to an expected value.The comparison uses `` == `` ( type coercion) instead of `` === `` ::
247
243
248
- $json = '{"a": [1, 2, 3]}';
244
+ $json = '{"a": [1, 2, 3]}';
249
245
250
- // passes because "1" == 1
251
- self::assertJsonPathEquals(['1'], '$.a[0]', $json);
246
+ // passes because "1" == 1
247
+ self::assertJsonPathEquals(['1'], '$.a[0]', $json);
252
248
253
249
* :method: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait::assertJsonPathNotEquals `
254
- Asserts that the result of a JSONPath expression is not equal ( ``!= ``) to an expected value ::
250
+ Asserts that the result of a JSONPath expression is not equal to an expected value.The comparison uses ``!= `` (type coercion) instead of `` !== `` ::
255
251
256
- $json = '{"a": [1, 2, 3]}';
257
- self::assertJsonPathNotEquals([42], '$.a[0]', $json);
252
+ $json = '{"a": [1, 2, 3]}';
253
+ self::assertJsonPathNotEquals([42], '$.a[0]', $json);
258
254
259
255
* :method: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait::assertJsonPathSame `
260
- Asserts that the result of a JSONPath expression is identical (``=== ``) to an expected value. This is a strict comparison and does not perform type coercion::
256
+ Asserts that the result of a JSONPath expression is identical (``=== ``) to an expected value. This is a strict comparison and does not perform type coercion::
261
257
262
- $json = '{"a": [1, 2, 3]}';
258
+ $json = '{"a": [1, 2, 3]}';
263
259
264
- // fails because "1" !== 1
265
- // self::assertJsonPathSame(['1'], '$.a[0]', $json);
260
+ // fails because "1" !== 1
261
+ // self::assertJsonPathSame(['1'], '$.a[0]', $json);
266
262
267
- self::assertJsonPathSame([1], '$.a[0]', $json);
263
+ self::assertJsonPathSame([1], '$.a[0]', $json);
268
264
269
265
* :method: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait::assertJsonPathNotSame `
270
- Asserts that the result of a JSONPath expression is not identical (``!== ``) to an expected value::
266
+ Asserts that the result of a JSONPath expression is not identical (``!== ``) to an expected value::
271
267
272
- $json = '{"a": [1, 2, 3]}';
273
- self::assertJsonPathNotSame(['1'], '$.a[0]', $json);
268
+ $json = '{"a": [1, 2, 3]}';
269
+ self::assertJsonPathNotSame(['1'], '$.a[0]', $json);
274
270
275
271
* :method: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait::assertJsonPathContains `
276
- Asserts that a given value is found within the array of results from the JSONPath expression::
272
+ Asserts that a given value is found within the array of results from the JSONPath expression::
277
273
278
- $json = '{"tags": ["php", "symfony", "json"]}';
279
- self::assertJsonPathContains('symfony', '$.tags[*]', $json);
274
+ $json = '{"tags": ["php", "symfony", "json"]}';
275
+ self::assertJsonPathContains('symfony', '$.tags[*]', $json);
280
276
281
277
* :method: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait::assertJsonPathNotContains `
282
- Asserts that a given value is NOT found within the array of results from the JSONPath expression::
278
+ Asserts that a given value is NOT found within the array of results from the JSONPath expression::
283
279
284
- $json = '{"tags": ["php", "symfony", "json"]}';
285
- self::assertJsonPathNotContains('java', '$.tags[*]', $json);
280
+ $json = '{"tags": ["php", "symfony", "json"]}';
281
+ self::assertJsonPathNotContains('java', '$.tags[*]', $json);
286
282
287
283
Error Handling
288
284
--------------
0 commit comments