File size: 9,250 Bytes
26e6f31 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | ---
title: JMESPath Functions
description: Utility
---
<!-- markdownlint-disable MD043 -->
???+ tip
JMESPath is a query language for JSON used by AWS CLI, AWS Python SDK, and Powertools for AWS Lambda (Python).
Built-in [JMESPath](https://jmespath.org/){target="_blank" rel="nofollow"} Functions to easily deserialize common encoded JSON payloads in Lambda functions.
## Key features
* Deserialize JSON from JSON strings, base64, and compressed data
* Use JMESPath to extract and combine data recursively
* Provides commonly used JMESPath expression with popular event sources
## Getting started
???+ tip
All examples shared in this documentation are available within the [project repository](https://github.com/aws-powertools/powertools-lambda-python/tree/develop/examples){target="_blank"}.
You might have events that contains encoded JSON payloads as string, base64, or even in compressed format. It is a common use case to decode and extract them partially or fully as part of your Lambda function invocation.
Powertools for AWS Lambda (Python) also have utilities like [validation](validation.md){target="_blank"}, [idempotency](idempotency.md){target="_blank"}, or [feature flags](feature_flags.md){target="_blank"} where you might need to extract a portion of your data before using them.
???+ info "Terminology"
**Envelope** is the terminology we use for the **JMESPath expression** to extract your JSON object from your data input. We might use those two terms interchangeably.
### Extracting data
You can use the `query` function with any [JMESPath expression](https://jmespath.org/tutorial.html){target="_blank" rel="nofollow"}.
???+ tip
Another common use case is to fetch deeply nested data, filter, flatten, and more.
=== "query.py"
```python hl_lines="1 6 10"
--8<-- "examples/jmespath_functions/src/query.py"
```
=== "extract_data_from_envelope.json"
```json
--8<-- "examples/jmespath_functions/src/extract_data_from_envelope.json"
```
### Built-in envelopes
We provide built-in envelopes for popular AWS Lambda event sources to easily decode and/or deserialize JSON objects.
=== "extract_data_from_builtin_envelope.py"
```python hl_lines="4-7 14"
--8<-- "examples/jmespath_functions/src/extract_data_from_builtin_envelope.py"
```
=== "extract_data_from_builtin_envelope.json"
```json hl_lines="6 15"
--8<-- "examples/jmespath_functions/src/extract_data_from_builtin_envelope.json"
```
These are all built-in envelopes you can use along with their expression as a reference:
| Envelope | JMESPath expression | |
| --------------------------------- | ----------------------------------------------------------------------------------------- |-|
| **`API_GATEWAY_HTTP`** | `powertools_json(body)` | |
| **`API_GATEWAY_REST`** | `powertools_json(body)` | |
| **`CLOUDWATCH_EVENTS_SCHEDULED`** | `detail` | |
| **`CLOUDWATCH_LOGS`** | `awslogs.powertools_base64_gzip(data) | powertools_json(@).logEvents[*]` |
| **`EVENTBRIDGE`** | `detail` | |
| **`KINESIS_DATA_STREAM`** | `Records[*].kinesis.powertools_json(powertools_base64(data))` | |
| **`S3_EVENTBRIDGE_SQS`** | `Records[*].powertools_json(body).detail` | |
| **`S3_KINESIS_FIREHOSE`** | `records[*].powertools_json(powertools_base64(data)).Records[0]` | |
| **`S3_SNS_KINESIS_FIREHOSE`** | `records[*].powertools_json(powertools_base64(data)).powertools_json(Message).Records[0]` | |
| **`S3_SNS_SQS`** | `Records[*].powertools_json(body).powertools_json(Message).Records[0]` | |
| **`S3_SQS`** | `Records[*].powertools_json(body).Records[0]` | |
| **`SNS`** | `Records[0].Sns.Message | powertools_json(@)` |
| **`SQS`** | `Records[*].powertools_json(body)` | |
???+ tip "Using SNS?"
If you don't require SNS metadata, enable [raw message delivery](https://docs.aws.amazon.com/sns/latest/dg/sns-large-payload-raw-message-delivery.html). It will reduce multiple payload layers and size, when using SNS in combination with other services (_e.g., SQS, S3, etc_).
## Advanced
### Built-in JMESPath functions
You can use our built-in JMESPath functions within your envelope expression. They handle deserialization for common data formats found in AWS Lambda event sources such as JSON strings, base64, and uncompress gzip data.
???+ info
We use these everywhere in Powertools for AWS Lambda (Python) to easily decode and unwrap events from Amazon API Gateway, Amazon Kinesis, AWS CloudWatch Logs, etc.
#### powertools_json function
Use `powertools_json` function to decode any JSON string anywhere a JMESPath expression is allowed.
> **Validation scenario**
This sample will deserialize the JSON string within the `data` key before validation.
=== "powertools_json_jmespath_function.py"
```python hl_lines="5 6 34 45 48 51"
--8<-- "examples/jmespath_functions/src/powertools_json_jmespath_function.py"
```
=== "powertools_json_jmespath_schema.py"
```python hl_lines="7 8 10 12 17 19 24 26 31 33 38 40"
--8<-- "examples/jmespath_functions/src/powertools_json_jmespath_schema.py"
```
=== "powertools_json_jmespath_payload.json"
```json
--8<-- "examples/jmespath_functions/src/powertools_json_jmespath_payload.json"
```
> **Idempotency scenario**
This sample will deserialize the JSON string within the `body` key before [Idempotency](./idempotency.md){target="_blank"} processes it.
=== "powertools_json_idempotency_jmespath.py"
```python hl_lines="16"
--8<-- "examples/jmespath_functions/src/powertools_json_idempotency_jmespath.py"
```
=== "powertools_json_idempotency_jmespath.json"
```json hl_lines="28"
--8<-- "examples/jmespath_functions/src/powertools_json_idempotency_jmespath.json"
```
#### powertools_base64 function
Use `powertools_base64` function to decode any base64 data.
This sample will decode the base64 value within the `data` key, and deserialize the JSON string before validation.
=== "powertools_base64_jmespath_function.py"
```python hl_lines="7 11 36 48 52 54 56"
--8<-- "examples/jmespath_functions/src/powertools_base64_jmespath_function.py"
```
=== "powertools_base64_jmespath_schema.py"
```python hl_lines="7 8 10 12 17 19 24 26 31 33 38 40"
--8<-- "examples/jmespath_functions/src/powertools_base64_jmespath_schema.py"
```
=== "powertools_base64_jmespath_payload.json"
```json
--8<-- "examples/jmespath_functions/src/powertools_base64_jmespath_payload.json"
```
#### powertools_base64_gzip function
Use `powertools_base64_gzip` function to decompress and decode base64 data.
This sample will decompress and decode base64 data from Cloudwatch Logs, then use JMESPath pipeline expression to pass the result for decoding its JSON string.
=== "powertools_base64_gzip_jmespath_function.py"
```python hl_lines="6 10 15 29 31 33 35"
--8<-- "examples/jmespath_functions/src/powertools_base64_gzip_jmespath_function.py"
```
=== "powertools_base64_gzip_jmespath_schema.py"
```python hl_lines="7-15 17 19 24 26 31 33 38 40"
--8<-- "examples/jmespath_functions/src/powertools_base64_gzip_jmespath_schema.py"
```
=== "powertools_base64_gzip_jmespath_payload.json"
```json
--8<-- "examples/jmespath_functions/src/powertools_base64_gzip_jmespath_payload.json"
```
### Bring your own JMESPath function
???+ warning
This should only be used for advanced use cases where you have special formats not covered by the built-in functions.
For special binary formats that you want to decode before applying JSON Schema validation, you can bring your own [JMESPath function](https://github.com/jmespath/jmespath.py#custom-functions){target="_blank" rel="nofollow"} and any additional option via `jmespath_options` param. To keep Powertools for AWS Lambda (Python) built-in functions, you can subclass from `PowertoolsFunctions`.
Here is an example of how to decompress messages using [zlib](https://docs.python.org/3/library/zlib.html){target="_blank" rel="nofollow"}:
=== "powertools_custom_jmespath_function.py"
```python hl_lines="9 14 17-18 23 34 39 41 43"
--8<-- "examples/jmespath_functions/src/powertools_custom_jmespath_function.py"
```
=== "powertools_custom_jmespath_function.json"
```json
--8<-- "examples/jmespath_functions/src/powertools_custom_jmespath_function.json"
```
|