JMESPath Functions
Built-in JMESPath 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 expressions with popular event sources
Getting started¶
Installation¶
Install the utility in your project:
1 |
|
You might have events that contain 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 (TypeScript) also have utilities like idempotency where you might need to extract a portion of your data before using them.
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 extractDataFromEnvelope
function with any JMESPath expression.
Tip
Another common use case is to fetch deeply nested data, filter, flatten, and more.
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 |
|
1 2 3 4 5 6 7 8 |
|
Built-in envelopes¶
We provide built-in envelopes for popular AWS Lambda event sources to easily decode and/or deserialize JSON objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
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) |
Using SNS?
If you don't require SNS metadata, enable raw message delivery. 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.
powertools_json
function¶
Use powertools_json
function to decode any JSON string anywhere a JMESPath expression is allowed.
Idempotency scenario
This sample will deserialize the JSON string within the body
key before Idempotency processes it.
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 |
|
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 |
|
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 processing.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
- The
data
variable contains the decoded object that looks like this:1 2 3 4 5 6 7
{ user_id: 123, product_id: 1, quantity: 2, price: 10.4, currency: 'USD', }
1 2 3 |
|
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
- The
payload
key contains a JSON object that once decompressed and decoded looks like this: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
{ "owner": "123456789012", "logGroup": "/aws/lambda/powertools-example", "logStream": "2020/09/02/[$LATEST]d3a8dcaffc7f4de2b8db132e3e106660", "subscriptionFilters": ["Destination"], "messageType": "DATA_MESSAGE", "logEvents": [ { "id": "eventId1", "message": { "username": "lessa", "message": "hello world" }, "timestamp": 1440442987000 }, { "id": "eventId2", "message": { "username": "dummy", "message": "hello world" }, "timestamp": 1440442987001 } ] }
- The
logGroup
variable contains the string"/aws/lambda/powertools-example"
.
1 2 3 |
|
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 processing, you can bring your own JMESPath function by extending the PowertoolsFunctions
class.
Here is an example of how to decompress messages compressed using the Brotli compression algorithm:
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 |
|
- The function signature can be enforced at runtime by using the
@Functions.signature
decorator. - The name of the function must start with the
func
prefix.
1 2 3 4 5 6 7 8 9 |
|