1. Home
  2. FlexRule Designer
  3. Expressions
  4. Collection search using JSONPath

Collection search using JSONPath

◷ Reading Time: 2 minutes

You can use the JSONPath syntax elements to search for data in a collection/ JSON object.

JSONPath SyntaxDescription
$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

JSONPathResult
1$.Classroom.Student[*].NameThe names of all the students
2$..NameAll the names
3$.Classroom.*All attributes in classroom which are students and teacher
4$.Classroom..SubjectAll 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]
$..Student[:2]
The first two students
8$..*All members of JSON structure

The attached FlexRule project demonstrates the above examples.

Updated on February 7, 2020

Article Attachments

Was this article helpful?

Related Articles