◷ Reading Time: 2 minutes
You can use the JSONPath syntax elements to search for data in a collection/ JSON object.
JSONPath Syntax | Description |
$ | the root object/element |
. or [] | child operator |
.. | recursive descent |
* | wildcard. All objects/elements regardless their names. |
[,] | Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set. |
Let’s take the following example.
{
"Classroom": {
"Student": [
{
"Name": "Sarah",
"Subject": "Maths",
"Score": 98
},
{
"Name": "Alex",
"Subject": "Physics",
"Score": 95
},
{
"Name": "Emma",
"Subject": "Biology",
"Score": 86
},
{
"Name": "John",
"Subject": "History",
"Score": 90
}
],
"Teacher": {
"Name": "Sharon" ,
"Subject": "Music"
}
}
}
If you have assigned the Classroom
to a variable named ClassroomInfo
, this will be the format.
ClassroomInfo|query(<JSONPath>)
For example, to get all the subjects,
ClassroomInfo|query("$..Subject")
The following table shows different ways of JSONPath queries you can apply
JSONPath | Result | |
1 | $.Classroom.Student[*].Name | The names of all the students |
2 | $..Name | All the names |
3 | $.Classroom.* | All attributes in classroom which are students and teacher |
4 | $.Classroom..Subject | All the subjects in classroom (students’and the teacher’s) |
5 | $..Student[2] | The third student |
6 | $..Student[-1:] | The last student |
7 | $..Student[0,1] | The first two students |
8 | $..* | All members of JSON structure |
The attached FlexRule project demonstrates the above examples.