-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
37 lines (26 loc) · 962 Bytes
/
handler.py
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
import json
from jsonpath_ng.ext import parse
FORWARD_KEY = '_forward'
def handle(event, context):
print("input r:" + json.dumps(event))
result = {}
forward = FORWARD_KEY in event
print('forward field from input enabled?: {}'.format(forward))
for key in sorted(event):
try:
if key.startswith('expression_'):
field = key[11:]
jsonpath_expr = parse(event[key])
matches = jsonpath_expr.find(event)
if matches and matches[0]:
result[field] = matches[0].value
continue
if key.startswith('unescape_'):
field = key[9:]
result[field] = json.loads(event[key])
continue
if forward and key != FORWARD_KEY:
result[key] = event[key]
except:
print('failed to process {}={}'.format(key, event[key]))
return result