...
/JSON and JMESPath: How Do We Filter the Output?
JSON and JMESPath: How Do We Filter the Output?
Learn to use JMESPath to filter the output of the AWS CLI.
We'll cover the following...
In the last lesson, we used the --query parameter to instruct the AWS CLI to only return a subset of the information.
In this lesson, we’ll take a deeper dive to understand how it works.
JMESPath
By default, the AWS CLI returns JSON (JavaScript Object Notation) data. To filter this output, it would be useful to use a JSON-specific query language, and this is where JMESPath comes in—it is a query language for JSON.
If we pass a value to the --query parameter, the AWS CLI interprets it as a JMESPath query and applies it to the original output of the command.
JSON basics
JSON has a tree-like structure with nested objects. Each element of a JSON tree can be of one of the following data types:
- Number
- String
- Boolean
- Array
- Object
- NULL
The most common data types we will encounter when working with the AWS CLI are strings, key-value pairs, objects, and arrays.
Key-value pairs
Key-value pairs are heavily used in JSON and look like this:
"InstanceId": "i-0c8082f95b289cfe5"
In this case, the key is InstanceId, and the value is i-0c8082f95b289cfe5.
Objects
Objects are a collection of data types. The syntax to create an object is the typical pair of curly brackets: {}.
Here’s an example of a JSON object from the last lesson:
"CurrentState": {"Code": 32,"Name": "shutting-down"}
Here, the object starts after "CurrentState": with the opening curly bracket ({ ...