1

I am receiving a payload via API Gateway to my Lambda function.

{
"resource": "/onfleet-webhook-production",
"path": "/onfleet-webhook-production",
"httpMethod": "POST",
"headers": {
    "accept": "application/json",
    "content-type": "application/json",
    "Host": "fje4zhwf83.execute1-api.us-east-1.amazonaws.com",
    "X-Amzn-Trace-Id": "Root=1-60884247-298cc6854b722b00668b0553",
    "X-Forwarded-For": "54.84.188.171",
    "X-Forwarded-Port": "443",
    "X-Forwarded-Proto": "https",
    "X-Onfleet-Signature": "539370210b482cbb4f7d728ecb33a690c89ff70dc574475000788d0fedfdd25471a45e909f42a951d8ccbb53c22ab0a548f3c7a7fa965b697ab13544c92ff08b"
},
"multiValueHeaders": {
    "accept": ["application/json"],
    "content-type": ["application/json"],
    "Host": ["fje4zhwf83.execute-api.us-east-1.amazonaws.com"],
    "X-Amzn-Trace-Id": ["Root=1-60884247-298cc6854b722b00668b0553"],
    "X-Forwarded-For": ["54.84.188.171"],
    "X-Forwarded-Port": ["443"],
    "X-Forwarded-Proto": ["https"],
    "X-Onfleet-Signature": ["539370210b482cbb4f7d728ecb33a690c89ff70dc574475000788d0fedfdd25471a45e909f42a951d8ccbb53c22ab0a548f3c7a7fa965b697ab13544c92ff08b"]
},
"queryStringParameters": null,
"multiValueQueryStringParameters": null,
"pathParameters": null,
"stageVariables": null,
"requestContext": {
    "resourceId": "0xos2v",
    "resourcePath": "/onfleet-webhook-production",
    "httpMethod": "POST",
    "extendedRequestId": "ec9LLEw0oAMFkwA=",
    "requestTime": "27/Apr/2021:16:56:39 +0000",
    "path": "/default/onfleet-webhook-production",
    "accountId": "406135884221",
    "protocol": "HTTP/1.1",
    "stage": "default",
    "domainPrefix": "fje4zhwf83",
    "requestTimeEpoch": 1619542599421,
    "requestId": "9485a44a-44f7-436c-9e98-0cb7d9034513",
    "identity": {
        "cognitoIdentityPoolId": null,
        "accountId": null,
        "cognitoIdentityId": null,
        "caller": null,
        "sourceIp": "54.84.188.171",
        "principalOrgId": null,
        "accessKey": null,
        "cognitoAuthenticationType": null,
        "cognitoAuthenticationProvider": null,
        "userArn": null,
        "userAgent": null,
        "user": null
    },
    "domainName": "fje4zhwf83.execut1e-api.us-east-1.amazonaws.com",
    "apiId": "fje4zhwf83"
},
"body": "{\"taskId\":\"f7XbmRxXvfntB086PseFXpZ9\",\"actionContext\":{\"type\":\"WORKER\",\"id\":\"C~s63JVCJTtD12J4BCY2Y4cN\"},\"triggerId\":0,\"triggerName\":\"taskStarted\",\"workerId\":\"C~s63JVCJTtD12J4BCY2Y4cN\",\"adminId\":null,\"data\":{\"task\":{\"id\":\"f7XbmRxXvfntB086PseFXpZ9\",\"timeCreated\":1619535291000,\"timeLastModified\":1619535394486,\"organization\":\"hFz5MmFqcMoMwjYmvvPbBo5U\",\"shortId\":\"4334535c\",\"trackingURL\":\"https://onf.lt/4334535c0c\",\"worker\":\"C~s63JVCJTtD12J4BCY2Y4cN\",\"merchant\":\"hFz5MmFqcMoMwjYmvvPbBo5U\",\"executor\":\"hFz5MmFqcMoMwjYmvvPbBo5U\",\"creator\":\"YG5INuyrKz4dggsW7Vo0q*Nv\",\"dependencies\":[],\"state\":2,\"completeAfter\":null,\"completeBefore\":null,\"pickupTask\":false,\"notes\":\"\",\"completionDetails\":{\"failureNotes\":\"\",\"failureReason\":\"NONE\",\"events\":[],\"actions\":[],\"time\":null,\"firstLocation\":[],\"lastLocation\":[],\"unavailableAttachments\":[]},\"feedback\":[],\"metadata\":[],\"overrides\":{},\"quantity\":0,\"serviceTime\":0,\"identity\":{\"failedScanCount\":0,\"checksum\":null},\"appearance\":{\"triangleColor\":null},\"container\":{\"type\":\"WORKER\",\"worker\":\"C~s63JVCJTtD12J4BCY2Y4cN\"},\"trackingViewed\":false,\"estimatedCompletionTime\":null,\"estimatedArrivalTime\":null,\"destination\":{\"id\":\"yIDUKM65Ck8bwNEP8z4W42Ea\",\"timeCreated\":1619535291000,\"timeLastModified\":1619535291451,\"location\":[-71.25429253636486,42.244868727926736],\"address\":{\"apartment\":\"\",\"state\":\"Massachusetts\",\"postalCode\":\"02030\",\"number\":\"35\",\"street\":\"Strawberry Hill Street\",\"city\":\"Dover\",\"country\":\"United States\"},\"notes\":\"\",\"metadata\":[],\"googlePlaceId\":null,\"warnings\":[]},\"recipients\":[{\"id\":\"5DoYN3*udPr8eUuEELGPq6yk\",\"timeCreated\":1571336170000,\"timeLastModified\":1619535291465,\"name\":\"s Test\",\"phone\":\"+16378343616\",\"notes\":\"Testing Email\",\"organization\":\"hFz5MmFqcMoMwjYmvvPbBo5U\",\"skipSMSNotifications\":false,\"metadata\":[]}]}},\"time\":1619535394615}",
"isBase64Encoded": false

}

I can access the values within this JSON; ex . data = event.get('path')

however, i can not acess to any of the variables inside the 'body'

`event.get('body')` doesn't seem to work at all

how can I access the values within the body like 'taskId', 'timeCreated" etc..

I have tried different things like json.loads(event). but they all end up with the same outcome.

1 Answer 1

1

Your body is a string. You have to use ast parse it to valid python map:

import ast

body = ast.literal_eval(event['body'])

then you should be able to use body as regular map.

Alternative

import json

body = json.loads("{}".format(event['body']))
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the response. Unfortunately, I am now receiving this error; ValueError: malformed node or string: <ast.Name object at 0x0000019FA90F1C10>
@AAA What generated the body? Is it AWS service, or some third party app?
It is a 3rd party tool called OnFleet generates the payload, and it calls our web hook at AWS ( API Gateway then our Lambda (Python) function.
@AAA I provided alternative. Can you please check it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.