Using jsonpath in fastjson to process JSON data

Time:2020-5-19

introduce

The version after fastjson 1.2.0 supports jsonpath. , which can be used as the JSON object query language (OQL) in the Java framework.

Common API

public class JSONPath {          
     //Evaluation, static method
     public static Object eval(Object rootObject, String path);

     //Evaluation, static method,按需计算,性能更好
     public static Object extract(String json, String path);
     
     //Calculate size, number of map non empty elements, number of object non empty elements, size of collection, and length of array. Other cannot be evaluated return - 1
     public static int size(Object rootObject, String path);
     
     //Include or not, object in path
     public static boolean contains(Object rootObject, String path) { }
     
     //Include or not, whether the specified value exists in the path, if it is a collection or an array, check whether the value exists in the collection
     public static boolean containsValue(Object rootObject, String path, Object value) { }
     
     //Modify the value of the specified path. If the modification is successful, return true, otherwise return false
     public static boolean set(Object rootObject, String path, Object value) {}

     //Add elements to an array or collection
     public static boolean arrayAdd(Object rootObject, String path, Object... values);
     
     //Get, the keyset of map, the name of the object's non empty property. Array, collection, etc. do not support null return of type.
     public static Set<?> keySet(Object rootObject, String path);
}

use

Prepare JSON as follows:

{"arr":[{"name":"zpl0","id":0},{"name":"zpl1","id":1},{"name":"zpl2","id":2},{"name":"zpl3","id":3},{"name":"zpl4","id":4}],"name":"zpl","id":1}

Value operation

Object rootName = JSONPath.eval(json,"$.name");
System.out.println(rootName);
Object rootArr = JSONPath.eval(json,"$.arr");
System.out.println(rootArr);

Output:
zpl
[{"name":"zpl0","id":0},{"name":"zpl1","id":1},{"name":"zpl2","id":2},{"name":"zpl3","id":3},{"name":"zpl4","id":4}]

Judgment operation

//Judge whether there is content with ID > 2
boolean isExist = JSONPath.contains(json,"$[id>0]");
System.out.println(isExist);
//Judge whether there is content with ID > 6
isExist = JSONPath.contains(json,"$[id>1]");
System.out.println(isExist);

output
true
false

Size operation

int size = JSONPath.size(json,"$.arr");
System.out.println(size);

output
5

When writing here, it is impossible to enumerate APIs and expressions one by one. The expressions are as follows:

JSONPath describe
$ Root object, for example $. Name
[num] Array access, where num is a number and can be a negative number. For example $[0]. Leader. Departments [- 1]. Name
[num0,num1,num2…] Array access to multiple elements, where num is a number, can be a negative number, return multiple elements in the array. For example $[0,3, – 2,5]
[start:end] Array range access, where start and end are start small tables and end subscripts, which can be negative numbers and return multiple elements in the array. For example $[0:5]
[start:end :step] Array range access, where start and end are start small tables and end subscripts, which can be negative numbers; step is step size, which returns multiple elements in the array. For example $[0:5:2]
[?(key)] Non empty filtering of object properties, such as $. Departments [? (name)]
[key > 123] For example, $. Departments [ID > = 123], the comparison operators support =,! =, >, > =, <<=
[key = ‘123’] String type object property comparison filtering, for example $. Departments [name = ‘123’], comparison operators support =,! =, >, > =, <<=
[key like ‘aa%’] String type like filtering, for example $. Departments [name like ‘SZ *’], wildcard only supports% not like
[key rlike ‘regexpr’] String type regular matching filtering, for example, parts [name like ‘AA (.) *’], regular syntax is JDK regular syntax, supporting not rlike
[key in (‘v0’, ‘v1’)] In filtering, support string and numeric type, for example: $. Departments [name in (‘wenshao ‘,’ yako ‘)] $. Departments [id not in (101102))
[key between 234 and 456] Between filtering, support of numerical type, support of not between for example: $. Departments [ID between 101 and 201] $. Departments [id not between 101 and 201]
Length() or size() Array length. For example $. Values. Size() supports types Java. Util. Map and Java. Util. Collection and arrays
keySet() Gets the keyset of the map or the non empty property name of the object. For example, $. Val.keyset() support type: map and normal object do not support: collection and array (return null)
. Property access, for example $. Name
.. Deepscan property access, for example $.. name
  • |All properties of the object, such as $. Leader*

[‘key ‘] | property access. For example $[‘name ‘]
[‘key0 ‘,’ key1 ‘] | multiple attribute access. For example $[‘id ‘,’ name ‘]

For more information, please refer to GitHub website: https://github.com/alibaba/fa

Recommended Today

JS function

1. Ordinary function Grammar: Function function name (){ Statement block } 2. Functions with parameters Grammar: Function function name (parameter list){ Statement block } 3. Function with return value Grammar: Function function name (parameter list){ Statement block; Return value; } Allow a variable to accept the return value after calling the function Var variable name […]