@@ -28,16 +28,8 @@ You can use the `extract_data_from_envelope` function along with any [JMESPath e
28
28
29
29
=== "app.py"
30
30
31
- ```python hl_lines="1 7"
32
- from aws_lambda_powertools.utilities.jmespath_utils import extract_data_from_envelope
33
-
34
- from aws_lambda_powertools.utilities.typing import LambdaContext
35
-
36
-
37
- def handler(event: dict, context: LambdaContext):
38
- payload = extract_data_from_envelope(data=event, envelope="powertools_json(body)")
39
- customer = payload.get("customerId") # now deserialized
40
- ...
31
+ ```python hl_lines="1 6"
32
+ --8<-- "docs/examples/utilities/jmespath_functions/extract_data_jmespath.py"
41
33
```
42
34
43
35
=== "event.json"
@@ -54,16 +46,8 @@ We provide built-in envelopes for popular JMESPath expressions used when looking
54
46
55
47
=== "app.py"
56
48
57
- ```python hl_lines="1 7"
58
- from aws_lambda_powertools.utilities.jmespath_utils import extract_data_from_envelope, envelopes
59
-
60
- from aws_lambda_powertools.utilities.typing import LambdaContext
61
-
62
-
63
- def handler(event: dict, context: LambdaContext):
64
- payload = extract_data_from_envelope(data=event, envelope=envelopes.SNS)
65
- customer = payload.get("customerId") # now deserialized
66
- ...
49
+ ```python hl_lines="1 6"
50
+ --8<-- "docs/examples/utilities/jmespath_functions/extract_data_built_in_jmespath.py"
67
51
```
68
52
69
53
=== "event.json"
@@ -107,6 +91,7 @@ Envelope | JMESPath expression
107
91
## Advanced
108
92
109
93
### Built-in JMESPath functions
94
+
110
95
You can use our built-in JMESPath functions within your expressions to do exactly that to decode JSON Strings, base64, and uncompress gzip data.
111
96
112
97
???+ info
@@ -123,49 +108,21 @@ This sample will decode the value within the `data` key into a valid JSON before
123
108
=== "powertools_json_jmespath_function.py"
124
109
125
110
```python hl_lines="9"
126
- from aws_lambda_powertools.utilities.validation import validate
127
-
128
- import schemas
129
-
130
- sample_event = {
131
- 'data': '{"payload": {"message": "hello hello", "username": "blah blah"}}'
132
- }
133
-
134
- validate(event=sample_event, schema=schemas.INPUT, envelope="powertools_json(data)")
111
+ --8<-- "docs/examples/utilities/jmespath_functions/powertools_json_jmespath_function.py"
135
112
```
136
113
137
114
=== "schemas.py"
138
115
139
- ```python hl_lines="7 14 16 23 39 45 47 52 "
116
+ ```python hl_lines="8 10 17 34 36 41 "
140
117
--8<-- "docs/shared/validation_basic_jsonschema.py"
141
118
```
142
119
143
120
> ** Idempotency scenario**
144
121
145
122
This sample will decode the value within the ` body ` key of an API Gateway event into a valid JSON object to ensure the Idempotency utility processes a JSON object instead of a string.
146
123
147
- ``` python hl_lines="7" title="Deserializing JSON before using as idempotency key"
148
- import json
149
- from aws_lambda_powertools.utilities.idempotency import (
150
- IdempotencyConfig, DynamoDBPersistenceLayer, idempotent
151
- )
152
-
153
- persistence_layer = DynamoDBPersistenceLayer(table_name = " IdempotencyTable" )
154
- config = IdempotencyConfig(event_key_jmespath = " powertools_json(body)" )
155
-
156
- @idempotent (config = config, persistence_store = persistence_layer)
157
- def handler (event :APIGatewayProxyEvent, context ):
158
- body = json.loads(event[' body' ])
159
- payment = create_subscription_payment(
160
- user = body[' user' ],
161
- product = body[' product_id' ]
162
- )
163
- ...
164
- return {
165
- " payment_id" : payment.id,
166
- " message" : " success" ,
167
- " statusCode" : 200
168
- }
124
+ ``` python hl_lines="6" title="Deserializing JSON before using as idempotency key"
125
+ -- 8 < -- " docs/examples/utilities/jmespath_functions/powertools_json_jmespath_function_idempotency.py"
169
126
```
170
127
171
128
#### powertools_base64 function
@@ -174,27 +131,15 @@ Use `powertools_base64` function to decode any base64 data.
174
131
175
132
This sample will decode the base64 value within the ` data ` key, and decode the JSON string into a valid JSON before we can validate it.
176
133
177
- === "powertools_json_jmespath_function .py"
134
+ === "powertools_base64_jmespath_function .py"
178
135
179
136
```python hl_lines="12"
180
- from aws_lambda_powertools.utilities.validation import validate
181
-
182
- import schemas
183
-
184
- sample_event = {
185
- "data": "eyJtZXNzYWdlIjogImhlbGxvIGhlbGxvIiwgInVzZXJuYW1lIjogImJsYWggYmxhaCJ9="
186
- }
187
-
188
- validate(
189
- event=sample_event,
190
- schema=schemas.INPUT,
191
- envelope="powertools_json(powertools_base64(data))"
192
- )
137
+ --8<-- "docs/examples/utilities/jmespath_functions/powertools_base64_jmespath_function.py"
193
138
```
194
139
195
140
=== "schemas.py"
196
141
197
- ```python hl_lines="7 14 16 23 39 45 47 52 "
142
+ ```python hl_lines="8 10 17 34 36 41 "
198
143
--8<-- "docs/shared/validation_basic_jsonschema.py"
199
144
```
200
145
@@ -204,27 +149,15 @@ Use `powertools_base64_gzip` function to decompress and decode base64 data.
204
149
205
150
This sample will decompress and decode base64 data, then use JMESPath pipeline expression to pass the result for decoding its JSON string.
206
151
207
- === "powertools_json_jmespath_function .py"
152
+ === "powertools_base64_gzip_jmespath_function .py"
208
153
209
154
```python hl_lines="12"
210
- from aws_lambda_powertools.utilities.validation import validate
211
-
212
- import schemas
213
-
214
- sample_event = {
215
- "data": "H4sIACZAXl8C/52PzUrEMBhFX2UILpX8tPbHXWHqIOiq3Q1F0ubrWEiakqTWofTdTYYB0YWL2d5zvnuTFellBIOedoiyKH5M0iwnlKH7HZL6dDB6ngLDfLFYctUKjie9gHFaS/sAX1xNEq525QxwFXRGGMEkx4Th491rUZdV3YiIZ6Ljfd+lfSyAtZloacQgAkqSJCGhxM6t7cwwuUGPz4N0YKyvO6I9WDeMPMSo8Z4Ca/kJ6vMEYW5f1MX7W1lVxaG8vqX8hNFdjlc0iCBBSF4ERT/3Pl7RbMGMXF2KZMh/C+gDpNS7RRsp0OaRGzx0/t8e0jgmcczyLCWEePhni/23JWalzjdu0a3ZvgEaNLXeugEAAA=="
216
- }
217
-
218
- validate(
219
- event=sample_event,
220
- schema=schemas.INPUT,
221
- envelope="powertools_base64_gzip(data) | powertools_json(@)"
222
- )
155
+ --8<-- "docs/examples/utilities/jmespath_functions/powertools_base64_gzip_jmespath_function.py"
223
156
```
224
157
225
158
=== "schemas.py"
226
159
227
- ```python hl_lines="7 14 16 23 39 45 47 52 "
160
+ ```python hl_lines="8 10 17 34 36 41 "
228
161
--8<-- "docs/shared/validation_basic_jsonschema.py"
229
162
```
230
163
@@ -239,25 +172,8 @@ In order to keep the built-in functions from Powertools, you can subclass from `
239
172
240
173
=== "custom_jmespath_function.py"
241
174
242
- ```python hl_lines="2-3 6-9 11 17"
243
- from aws_lambda_powertools.utilities.jmespath_utils import (
244
- PowertoolsFunctions, extract_data_from_envelope)
245
- from jmespath.functions import signature
246
-
247
-
248
- class CustomFunctions(PowertoolsFunctions):
249
- @signature({'types': ['string']}) # Only decode if value is a string
250
- def _func_special_decoder(self, s):
251
- return my_custom_decoder_logic(s)
252
-
253
- custom_jmespath_options = {"custom_functions": CustomFunctions()}
254
-
255
- def handler(event, context):
256
- # use the custom name after `_func_`
257
- extract_data_from_envelope(data=event,
258
- envelope="special_decoder(body)",
259
- jmespath_options=**custom_jmespath_options)
260
- ...
175
+ ```python hl_lines="1 3 6-9 12 20"
176
+ --8<-- "docs/examples/utilities/jmespath_functions/custom_jmespath_function.py"
261
177
```
262
178
263
179
=== "event.json"
0 commit comments