Skip to content

Commit eadcd79

Browse files
Defines the pattern for taking unique items from collection of JSONArray
1 parent 21de620 commit eadcd79

File tree

5 files changed

+56
-15
lines changed

5 files changed

+56
-15
lines changed

README.md

+15-14
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,21 @@ Functions
8585
Functions can be invoked at the tail end of a path - the input to a function is the output of the path expression.
8686
The function output is dictated by the function itself.
8787

88-
| Function | Description | Output type |
89-
|:----------|:-------------------------------------------------------------------------------------|:---------------------|
90-
| min() | Provides the min value of an array of numbers | Double |
91-
| max() | Provides the max value of an array of numbers | Double |
92-
| avg() | Provides the average value of an array of numbers | Double |
93-
| stddev() | Provides the standard deviation value of an array of numbers | Double |
94-
| length() | Provides the length of an array | Integer |
95-
| sum() | Provides the sum value of an array of numbers | Double |
96-
| keys() | Provides the property keys (An alternative for terminal tilde `~`) | `Set<E>` |
97-
| concat(X) | Provides a concatinated version of the path output with a new item | like input |
98-
| append(X) | add an item to the json path output array | like input |
99-
| first() | Provides the first item of an array | Depends on the array |
100-
| last() | Provides the last item of an array | Depends on the array |
101-
| index(X) | Provides the item of an array of index: X, if the X is negative, take from backwards | Depends on the array |
88+
| Function | Description | Output type |
89+
|:-----------|:-------------------------------------------------------------------------------------|:---------------------|
90+
| min() | Provides the min value of an array of numbers | Double |
91+
| max() | Provides the max value of an array of numbers | Double |
92+
| avg() | Provides the average value of an array of numbers | Double |
93+
| stddev() | Provides the standard deviation value of an array of numbers | Double |
94+
| length() | Provides the length of an array | Integer |
95+
| sum() | Provides the sum value of an array of numbers | Double |
96+
| keys() | Provides the property keys (An alternative for terminal tilde `~`) | `Set<E>` |
97+
| concat(X) | Provides a concatinated version of the path output with a new item | like input |
98+
| append(X) | add an item to the json path output array | like input |
99+
| first() | Provides the first item of an array | Depends on the array |
100+
| last() | Provides the last item of an array | Depends on the array |
101+
| index(X) | Provides the item of an array of index: X, if the X is negative, take from backwards | Depends on the array |
102+
| distinct() | Provides the unique items of an array | Depends on the array |
102103
Filter Operators
103104
-----------------
104105

json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunctionFactory.java

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.jayway.jsonpath.internal.function.numeric.Min;
99
import com.jayway.jsonpath.internal.function.numeric.StandardDeviation;
1010
import com.jayway.jsonpath.internal.function.numeric.Sum;
11+
import com.jayway.jsonpath.internal.function.sequence.Distinct;
1112
import com.jayway.jsonpath.internal.function.sequence.First;
1213
import com.jayway.jsonpath.internal.function.sequence.Index;
1314
import com.jayway.jsonpath.internal.function.sequence.Last;
@@ -53,6 +54,7 @@ public class PathFunctionFactory {
5354
// Sequential Functions
5455
map.put("first", First.class);
5556
map.put("last", Last.class);
57+
map.put("distinct", Distinct.class);
5658
map.put("index", Index.class);
5759

5860

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.jayway.jsonpath.internal.function.sequence;
2+
3+
import com.jayway.jsonpath.JsonPathException;
4+
import com.jayway.jsonpath.internal.EvaluationContext;
5+
import com.jayway.jsonpath.internal.PathRef;
6+
import com.jayway.jsonpath.internal.function.Parameter;
7+
import com.jayway.jsonpath.internal.function.PathFunction;
8+
import java.util.ArrayList;
9+
import java.util.HashSet;
10+
import java.util.List;
11+
import java.util.Set;
12+
13+
/**
14+
* Take the unique items from collection of JSONArray
15+
*
16+
* Created by PavelSakharchuk on 21/09/23
17+
*/
18+
public class Distinct implements PathFunction {
19+
20+
@Override
21+
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
22+
if(ctx.configuration().jsonProvider().isArray(model)){
23+
Iterable<?> objects = ctx.configuration().jsonProvider().toIterable(model);
24+
Set<Object> objectSet = new HashSet<>();
25+
objects.forEach(objectSet::add);
26+
27+
return new ArrayList<>(objectSet);
28+
}
29+
throw new JsonPathException("Aggregation function attempted to calculate value using empty array");
30+
}
31+
}

json-path/src/test/java/com/jayway/jsonpath/internal/function/BaseFunctionTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
public class BaseFunctionTest {
1515
protected static final String NUMBER_SERIES = "{\"empty\": [], \"numbers\" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}";
16-
protected static final String TEXT_SERIES = "{\"urls\": [\"http://api.worldbank.org/countries/all/?format=json\", \"http://api.worldbank.org/countries/all/?format=json\"], \"text\" : [ \"a\", \"b\", \"c\", \"d\", \"e\", \"f\" ]}";
16+
protected static final String TEXT_SERIES = "{\"urls\": [\"http://api.worldbank.org/countries/all/?format=json\", \"http://api.worldbank.org/countries/all/?format=json\"], \"text\" : [ \"a\", \"b\", \"c\", \"d\", \"d\", \"e\", \"f\" ]}";
1717
protected static final String TEXT_AND_NUMBER_SERIES = "{\"text\" : [ \"a\", \"b\", \"c\", \"d\", \"e\", \"f\" ], \"numbers\" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}";
1818

1919
/**

json-path/src/test/java/com/jayway/jsonpath/internal/function/SequentialPathFunctionTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.jayway.jsonpath.Configuration;
44
import com.jayway.jsonpath.Configurations;
5+
import java.util.Arrays;
56
import org.junit.Test;
67

78
/**
@@ -10,6 +11,7 @@
1011
* -first
1112
* -last
1213
* -index(X)
14+
* -distinct
1315
*
1416
* Created by git9527 on 6/11/22.
1517
*/
@@ -50,4 +52,9 @@ public void testIndexOfText() throws Exception {
5052
verifyFunction(conf, "$.text.index(-1)", TEXT_SERIES, "f");
5153
verifyFunction(conf, "$.text.index(1)", TEXT_SERIES, "b");
5254
}
55+
56+
@Test
57+
public void testDistinctOfText() throws Exception {
58+
verifyFunction(conf, "$.text.distinct()", TEXT_SERIES, Arrays.asList("a", "b", "c", "d", "e", "f"));
59+
}
5360
}

0 commit comments

Comments
 (0)