Powertools for AWS Lambda (.NET) - JMESPath package.
$ dotnet add package AWS.Lambda.Powertools.JMESPathJMESPath is a query language for JSON used by AWS CLI, AWS Python SDK, and Powertools for AWS Lambda.
With built-in JMESPath functions to easily deserialize common encoded JSON payloads in Lambda functions.
JMESPath allows you to transform a JsonDocument into another JsonDocument.
For example, consider the JSON data
string jsonString = """
{
"body": "{\"customerId\":\"dd4649e6-2484-4993-acb8-0f9123103394\"}",
"deeply_nested": [
{
"some_data": [
1,
2,
3
]
}
]
}
""";
using JsonDocument doc = JsonDocument.Parse(jsonString);
string expr = "powertools_json(body).customerId";
//also works for fetching and flattening deeply nested data
// string expr = "deeply_nested[*].some_data[]";
JsonDocument result = JsonTransformer.Transform(doc.RootElement, expr);
It produces the result
"dd4649e6-2484-4993-acb8-0f9123103394"
You can find more examples here
We provide built-in envelopes for popular AWS Lambda event sources to easily decode and/or deserialize JSON objects.
| Envelop | JMESPath expression |
|---|---|
| API_GATEWAY_HTTP | powertools_json(body) |
| API_GATEWAY_REST | powertools_json(body) |
| CLOUDWATCH_LOGS | awslogs.powertools_base64_gzip(data) | powertools_json(@).logEvents[*] |
| KINESIS_DATA_STREAM | Records[*].kinesis.powertools_json(powertools_base64(data)) |
| SNS | Records[*].Sns.Message | powertools_json(@) |
| SQS | Records[*].powertools_json(body) |
More examples of events can be found here
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.
Use powertools_json function to decode any JSON string anywhere a JMESPath expression is allowed.
Use powertools_base64 function to decode any base64 data.
Use powertools_base64_gzip function to decompress and decode base64 data.
We took heavy inspiration in the https://github.com/danielaparker/JsonCons.Net repository.