Skip to content

Commit b5b2f1f

Browse files
committed
* JsonPathEvaluator::setValues() now returns the number of values actually set
* JsonPathEvaluator::deletePaths() now returns the number of paths deleted
1 parent ff3eea9 commit b5b2f1f

File tree

4 files changed

+172
-185
lines changed

4 files changed

+172
-185
lines changed

README.md

Lines changed: 39 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ composer require ropi/json-path-evaluator
3333
The following example shows how to get values that match a JSONPath.\
3434
The result is always an array of values that match the JSONPath. If there are no matches, an empty array is returned.
3535
```php
36-
<?php
3736
$data = json_decode('{ "store": {
3837
"book": [
3938
{ "category": "reference",
@@ -66,91 +65,50 @@ $data = json_decode('{ "store": {
6665
}
6766
}');
6867

69-
7068
$evaluator = new \Ropi\JsonPathEvaluator\JsonPathEvaluator();
7169

72-
73-
echo "Get authors of all books in the store:\n";
74-
7570
$result = $evaluator->getValues($data, '$.store.book[*].author');
76-
77-
echo json_encode($result, JSON_PRETTY_PRINT) . "\n";
78-
79-
80-
81-
echo "Get the prices of everything in the store:\n";
71+
echo "Authors of all books in the store:\n" . json_encode($result, JSON_PRETTY_PRINT) . "\n";
8272

8373
$result = $evaluator->getValues($data, '$.store..price');
84-
85-
echo json_encode($result, JSON_PRETTY_PRINT) . "\n";
86-
87-
88-
89-
echo "Get the last book in order:\n";
74+
echo "Prices of everything in the store:\n" . json_encode($result, JSON_PRETTY_PRINT) . "\n";
9075

9176
$result = $evaluator->getValues($data, '$..book[-1]');
92-
93-
echo json_encode($result, JSON_PRETTY_PRINT) . "\n";
94-
95-
96-
97-
echo "Get the first two books with union operator:\n";
77+
echo "Last book in order:\n" . json_encode($result, JSON_PRETTY_PRINT) . "\n";
9878

9979
$result = $evaluator->getValues($data, '$..book[0,1]');
100-
101-
echo json_encode($result, JSON_PRETTY_PRINT) . "\n";
102-
103-
104-
105-
echo "Get the first two books with array slice operator:\n";
80+
echo "First two books with union operator:\n" . json_encode($result, JSON_PRETTY_PRINT) . "\n";
10681

10782
$result = $evaluator->getValues($data, '$..book[:2]');
108-
109-
echo json_encode($result, JSON_PRETTY_PRINT) . "\n";
110-
111-
112-
113-
echo "Get all books with an ISBN number:\n";
83+
echo "First two books with array slice operator:\n" . json_encode($result, JSON_PRETTY_PRINT) . "\n";
11484

11585
$result = $evaluator->getValues($data, '$..book[?@.isbn]');
116-
117-
echo json_encode($result, JSON_PRETTY_PRINT) . "\n";
118-
119-
120-
121-
echo "Get all books cheaper than 10:\n";
86+
echo "All books with an ISBN number:\n" . json_encode($result, JSON_PRETTY_PRINT) . "\n";
12287

12388
$result = $evaluator->getValues($data, '$..book[?@.price<10]');
89+
echo "All books cheaper than 10:\n" . json_encode($result, JSON_PRETTY_PRINT) . "\n";
12490
125-
echo json_encode($result, JSON_PRETTY_PRINT) . "\n";
126-
127-
128-
129-
echo "Get all books where ISBN ends with 1, 2 or 3 (Regular Expression):\n";
130-
131-
$result = $evaluator->getValues($data, '$..book[?search(@.isbn, "[1-3]$")]');
132-
133-
echo json_encode($result, JSON_PRETTY_PRINT) . "\n";
91+
$result = $evaluator->getValues($data, '$..book[?search(@.isbn, "[1-3]$")]'); // regular expression
92+
echo "All books where ISBN ends with 1, 2 or 3:\n" . json_encode($result, JSON_PRETTY_PRINT) . "\n";
13493
```
13594
The above example will output:
13695
```
137-
138-
Get authors of all books in the store:
96+
Authors of all books in the store:
13997
[
14098
"Nigel Rees",
14199
"Evelyn Waugh",
142100
"Herman Melville",
143101
"J. R. R. Tolkien"
144102
]
145-
Get the prices of everything in the store:
103+
Prices of everything in the store:
146104
[
147105
8.95,
148106
12.99,
149107
8.99,
150108
22.99,
151109
399
152110
]
153-
Get the last book in order:
111+
Last book in order:
154112
[
155113
{
156114
"category": "fiction",
@@ -160,7 +118,7 @@ Get the last book in order:
160118
"price": 22.99
161119
}
162120
]
163-
Get the first two books with union operator:
121+
First two books with union operator:
164122
[
165123
{
166124
"category": "reference",
@@ -175,7 +133,7 @@ Get the first two books with union operator:
175133
"price": 12.99
176134
}
177135
]
178-
Get the first two books with array slice operator:
136+
First two books with array slice operator:
179137
[
180138
{
181139
"category": "reference",
@@ -190,7 +148,7 @@ Get the first two books with array slice operator:
190148
"price": 12.99
191149
}
192150
]
193-
Get all books with an ISBN number:
151+
All books with an ISBN number:
194152
[
195153
{
196154
"category": "fiction",
@@ -207,7 +165,7 @@ Get all books with an ISBN number:
207165
"price": 22.99
208166
}
209167
]
210-
Get all books cheaper than 10:
168+
All books cheaper than 10:
211169
[
212170
{
213171
"category": "reference",
@@ -223,7 +181,7 @@ Get all books cheaper than 10:
223181
"price": 8.99
224182
}
225183
]
226-
Get all books where ISBN ends with 1, 2 or 3 (Regular Expression):
184+
All books where ISBN ends with 1, 2 or 3:
227185
[
228186
{
229187
"category": "fiction",
@@ -233,14 +191,12 @@ Get all books where ISBN ends with 1, 2 or 3 (Regular Expression):
233191
"price": 8.99
234192
}
235193
]
236-
237194
```
238195
239196
### Get paths
240197
The following example shows how to get value paths that match a JSONPath, where each value path is represented as normalized JSONPath according to section 2.7 of [JSONPath internet draft](https://datatracker.ietf.org/doc/draft-ietf-jsonpath-base/21/).\
241198
The result is always an array of matched paths. If there are no matches, an empty array is returned.
242199
```php
243-
<?php
244200
$data = json_decode('{ "store": {
245201
"book": [
246202
{ "category": "reference",
@@ -273,60 +229,43 @@ $data = json_decode('{ "store": {
273229
}
274230
}');
275231
276-
277232
$evaluator = new \Ropi\JsonPathEvaluator\JsonPathEvaluator();
278233
279-
280-
echo "Get authors of all books in the store:\n";
281-
282234
$result = $evaluator->getPaths($data, '$.store.book[*].author');
283-
284-
echo json_encode($result, JSON_PRETTY_PRINT) . "\n";
285-
286-
287-
288-
echo "Get the prices of everything in the store:\n";
235+
echo "Paths of authors of all books in store:\n" . json_encode($result, JSON_PRETTY_PRINT) . "\n";
289236
290237
$result = $evaluator->getPaths($data, '$.store..price');
291-
292-
echo json_encode($result, JSON_PRETTY_PRINT) . "\n";
293-
294-
295-
296-
echo "Get all books cheaper than 10:\n";
238+
echo "Paths of prices of everything in the store:\n" . json_encode($result, JSON_PRETTY_PRINT) . "\n";
297239
298240
$result = $evaluator->getPaths($data, '$..book[?@.price<10]');
299-
300-
echo json_encode($result, JSON_PRETTY_PRINT) . "\n";
241+
echo "Paths of all books cheaper than 10:\n" . json_encode($result, JSON_PRETTY_PRINT) . "\n";
301242
```
302243
The above example will output:
303244
```
304-
Get authors of all books in the store:
245+
Paths of authors of all books in store:
305246
[
306247
"$['store']['book'][0]['author']",
307248
"$['store']['book'][1]['author']",
308249
"$['store']['book'][2]['author']",
309250
"$['store']['book'][3]['author']"
310251
]
311-
Get the prices of everything in the store:
252+
Paths of prices of everything in the store:
312253
[
313254
"$['store']['book'][0]['price']",
314255
"$['store']['book'][1]['price']",
315256
"$['store']['book'][2]['price']",
316257
"$['store']['book'][3]['price']",
317258
"$['store']['bicycle']['price']"
318259
]
319-
Get all books cheaper than 10:
260+
Paths of all books cheaper than 10:
320261
[
321262
"$['store']['book'][0]",
322263
"$['store']['book'][2]"
323264
]
324-
325265
```
326266
### Set values
327267
The following example shows how to set/replace values.
328268
```php
329-
<?php
330269
$data = json_decode('{ "store": {
331270
"book": [
332271
{ "category": "reference",
@@ -359,27 +298,17 @@ $data = json_decode('{ "store": {
359298
}
360299
}');
361300
362-
363301
$evaluator = new \Ropi\JsonPathEvaluator\JsonPathEvaluator();
364302
303+
$numSet = $evaluator->setValues($data, '$..price', [10]);
304+
echo "Set all $numSet prices to 10:\n" . json_encode($data, JSON_PRETTY_PRINT) . "\n";
365305
366-
echo "Set all prices to 10:\n";
367-
368-
$evaluator->setValues($data, '$..price', [10]);
369-
370-
echo json_encode($data, JSON_PRETTY_PRINT) . "\n";
371-
372-
373-
374-
echo "Set prices alternately to 1, 2 and 3:\n";
375-
376-
$evaluator->setValues($data, '$..price', [1, 2, 3]);
377-
378-
echo json_encode($data, JSON_PRETTY_PRINT) . "\n";
306+
$numSet = $evaluator->setValues($data, '$..price', [1, 2, 3]);
307+
echo "Set all $numSet alternately to 1, 2 and 3:\n" . json_encode($data, JSON_PRETTY_PRINT) . "\n";
379308
```
380309
The above example will output:
381310
```
382-
Set all prices to 10:
311+
Set all 5 prices to 10:
383312
{
384313
"store": {
385314
"book": [
@@ -416,7 +345,7 @@ Set all prices to 10:
416345
}
417346
}
418347
}
419-
Set prices alternately to 1, 2 and 3:
348+
Set all 5 alternately to 1, 2 and 3:
420349
{
421350
"store": {
422351
"book": [
@@ -453,62 +382,49 @@ Set prices alternately to 1, 2 and 3:
453382
}
454383
}
455384
}
456-
457385
```
458386
### Set values and create non-existent paths
459387
The following example shows how to set values on non-existent paths.
460388
```php
461-
<?php
462389
$evaluator = new \Ropi\JsonPathEvaluator\JsonPathEvaluator();
463390
464-
465-
echo "Create non-existent paths as stdClass objects:\n";
391+
// Create non-existent paths as stdClass objects
466392
$data = json_decode('{}');
467-
468-
$evaluator->setValues($data, '$.deep.path.value', ["new-value"], \Ropi\JsonPathEvaluator\NonExistentPathBehavior::CreateStdClass);
469-
393+
$evaluator->setValues($data, '$.deep.path.value', ["stdClassExample"], \Ropi\JsonPathEvaluator\NonExistentPathBehavior::CreateStdClass);
470394
var_dump($data) . "\n";
471395
472-
473-
474-
echo "Create non-existent paths as arrays:\n";
396+
// Create non-existent paths as arrays
475397
$data = json_decode('[]');
476-
477-
$evaluator->setValues($data, '$.deep.path.value', ["new-value"], \Ropi\JsonPathEvaluator\NonExistentPathBehavior::CreateArray);
478-
398+
$evaluator->setValues($data, '$.deep.path.value', ["arrayExample"], \Ropi\JsonPathEvaluator\NonExistentPathBehavior::CreateArray);
479399
var_dump($data) . "\n";
480400
```
481401
The above example will output:
482402
```
483-
Create non-existent paths as stdClass objects:
484403
object(stdClass)#8 (1) {
485404
["deep"]=>
486405
object(stdClass)#37 (1) {
487406
["path"]=>
488407
object(stdClass)#32 (1) {
489408
["value"]=>
490-
string(9) "new-value"
409+
string(15) "stdClassExample"
491410
}
492411
}
493412
}
494-
Create non-existent paths as arrays:
495413
array(1) {
496414
["deep"]=>
497415
array(1) {
498416
["path"]=>
499417
array(1) {
500418
["value"]=>
501-
string(9) "new-value"
419+
string(12) "arrayExample"
502420
}
503421
}
504422
}
505-
506423
```
507424
### Delete paths
508425
The following example shows how to delete/remove/unset paths that match a JSONPath.
509426
510427
```php
511-
<?php
512428
$data = json_decode('{ "store": {
513429
"book": [
514430
{ "category": "reference",
@@ -541,19 +457,14 @@ $data = json_decode('{ "store": {
541457
}
542458
}');
543459
544-
545460
$evaluator = new \Ropi\JsonPathEvaluator\JsonPathEvaluator();
546461
547-
548-
echo "Delete all books that are more expensive than 9 euros:\n";
549-
550-
$evaluator->deletePaths($data, '$.store.book[@.price > 9]');
551-
552-
echo json_encode($data, JSON_PRETTY_PRINT) . "\n";
462+
$numDeleted = $evaluator->deletePaths($data, '$.store.book[?@.price > 9]');
463+
echo "Deleted all $numDeleted books that are more expensive than 9:\n" . json_encode($data, JSON_PRETTY_PRINT) . "\n";
553464
```
554465
The above example will output:
555466
```
556-
Delete all books that are more expensive than 9 euros:
467+
Deleted all 2 books in store that are more expensive than 9:
557468
{
558469
"store": {
559470
"book": {
@@ -577,7 +488,6 @@ Delete all books that are more expensive than 9 euros:
577488
}
578489
}
579490
}
580-
581491
```
582492
### Custom function extensions
583493
The following example shows how to register custom function extensions according to section 2.4 of [JSONPath internet draft](https://datatracker.ietf.org/doc/draft-ietf-jsonpath-base/21/).
@@ -605,13 +515,11 @@ $evaluator->registerFunction('myFunction', function(\Ropi\JsonPathEvaluator\Type
605515
});
606516

607517
$result = $evaluator->getValues($data, '$.values[?myFunction(@.property)].property');
608-
609-
echo json_encode($result, JSON_PRETTY_PRINT) . "\n";
518+
echo json_encode($result, JSON_PRETTY_PRINT);
610519
```
611520
The above example will output:
612521
```
613522
[
614523
"valueB"
615524
]
616-
617525
```

0 commit comments

Comments
 (0)