
YAML vs. JSON
In an earlier lesson, we explored the basics of YAML and how it represents data in a human-readable structured format. For example, a car can be represented in YAML as follows:Introduction to JSONPath
JSONPath is a query language used to extract data from JSON (or YAML) documents, much like SQL queries are used with relational databases. Consider the following JSON document containing details about a car and a bus:car. To obtain a specific property, use the dot notation (e.g., car.color for the car’s color). Similarly, use bus for bus details and bus.price for its price.
When these objects are nested within a parent dictionary (for example, named vehicles), the queries adjust accordingly. Consider this JSON structure:
- Get car details:
vehicles.car - Get bus details:
vehicles.bus - Get car’s color:
vehicles.car.color - Get bus’s price:
vehicles.bus.price
$). Thus, for the original document (without the vehicles parent), the correct queries are:
- Get car details:
$.car - Get bus details:
$.bus - Get car’s color:
$.car.color - Get bus’s price:
$.bus.price
vehicles, then the queries become $.vehicles.car, and so on.
The result of any JSONPath query is always returned as an array—even if it contains only a single element. For example, the query
$.vehicles.car returns:$.vehicles.car.color returns:
Working with Lists and Arrays
Consider a JSON array of vehicle types:- First element:
$[0]returns[ "car" ] - Fourth element:
$[3]returns[ "bike" ] - Multiple elements (e.g., first and fourth):
$[0,3]returns[ "car", "bike" ]
Querying Complex JSON Structures
Now, let’s look at querying a more complex JSON object. Consider a JSON document representing a car with multiple properties, including a list of wheels:Extracting a Specific Element by Index
To retrieve the details of the second wheel (remember, indexing starts at zero), use:Using Filter Criteria in JSONPath
When the order of array items might change, instead of hard-coding an index, you can use a filter criterion to select an element based on its property. For instance, to retrieve the model of the wheel where thelocation is “rear-right”, use:
location property equal to “rear-right”, and return their model values.
Applying Criteria to Query Arrays
Criteria (filters) are especially useful when working with arrays that contain many items. Consider the following array of numbers:@) represents each array element:
Practice Exercises
Now that you understand the basics of JSONPath, it’s time to practice! Visit the following link to access a quiz portal where you can apply your knowledge by creating JSONPath queries: JSONPath Quiz Portal In the quiz, you will see:- A list of questions on the left.
- An input area where you enter your JSONPath query.
- The source data displayed on the lower-left.
- The expected output on the right.
For further information about JSONPath, visit the official documentation on GitHub: JSONPath on GitHub. If you have any suggestions or would like to see advanced use cases in future lessons, please leave a comment below. Thank you for watching, and happy coding! Don’t forget to subscribe for more lessons like this. Goodbye!