JSONPath

From Derivative
Jump to navigation Jump to search

JSONPath is a query language for JSON. JSONPath allows you to search and filter out specific elements from a JSON structure.

The JSON DAT support JSONPath querying via its JSONPath Filter parameter.

For an in-depth overview of syntax, as well as more examples, see: https://pypi.org/project/jsonpath-ng/

Overview[edit]

Consider the following JSON example for the examples in the table:

{
   "school" :
   {
	    "students": [
	        {"name": "Peter", "gender": "Male", "age": 20},
 	        {"name": "Mary", "gender": "Female", "age": 22},
	        {"name": "Susan", "gender": "Female", "age": 29}
	    ],
	    "teachers": [
	        {"name": "William", "gender": "Male", "age": 33},
	        {"name": "John", "gender": "Male", "age": 38},
	        {"name": "Lucy", "gender": "Female", "age": 62}
	    ]
   }
}
Expression Description Example
$ The root object or array. $
.property or ['property'] Selects the property from the parent object. $.school.students or $['school']['students'] selects the array of students.
[n] Selects the nth element from an array object. $.school.students[0] selects the first student (ie. Peter).
[i,j,k,…] Selects object elements with the specified indices. $.school['students','teachers'] selects the students and teachers.
..property Recursively searches for the specified property $..students searches for the students array recursively from the root
* Selects everything in an array or object (wildcard). $.school.students[0].* selects and returns all info about the first student (Peter, "Male", 20).
[start:end] Selects a range from an array. $.school.students[1:3] selects students Mary and Susan
[:n] Selects the first n elements from an array. $.school.students[:2] selects students Peter and Mary
[-n:] Selects the last n elements from an array $.school.students[-1:] selects the student Susan
[?(expression)] Selects all elements in an array that satisfy the expression. $.school['students', 'teachers'][?(@.age>25)] selects all students and teachers over the age of 25.
@ For use in expressions. Refers to the current element being processed.