Skip to content
This repository has been archived by the owner on Jan 15, 2023. It is now read-only.

Unable to unmarshal input: Expecting value: line 1 column 1 (char 0) #208

Closed
cadeath opened this issue Aug 30, 2019 · 8 comments
Closed

Comments

@cadeath
Copy link

cadeath commented Aug 30, 2019

Hello Guys,

I just want to ask on how to test my Lambda function with an json input?

When I tested it without parameter (json input)
docker run --rm -v %cd%:/var/task lambci/lambda:python3.6

it succeed

but when these commands

docker run --rm -v %cd%:/var/task lambci/lambda:python3.6 {"msg":"hello"}
docker run --rm -v %cd%:/var/task lambci/lambda:python3.6 '{"msg":"hello"}'

output
{"errorMessage": "Bad handler ''{msg:hello}''"}

and this
docker run --rm -v %cd%:/var/task lambci/lambda:python3.6 lambda_function.lambda_handler '{"msg":"hello"}'

output
{"errorMessage": "Expecting value: line 1 column 1 (char 0)", "errorType": "JSONDecodeError", "stackTrace": [["/var/lang/lib/python3.6/json/__init__.py", 354, "loads", "return _default_decoder.decode(s)"], ["/var/lang/lib/python3.6/json/decoder.py", 339, "decode", "obj, end = self.raw_decode(s, idx=_w(s, 0).end())"], ["/var/lang/lib/python3.6/json/decoder.py", 357, "raw_decode", "raise JSONDecodeError(\"Expecting value\", s, err.value) from None"]]}

My codes are

import os
import sys

import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)

def lambda_handler(event, context):
    context.log('Hello!')
    context.log('Hmmm, does not add newlines in 3.7?')
    context.log('\n')
    print("This is from Print Command")
    logger.info("This is INFO")
    
    return "done"

Windows 10
Docker 2.1.0.1 (37199)

@mhart
Copy link
Member

mhart commented Aug 30, 2019

If you're not using a bash shell (I see you're on Windows) then you'll need to escape those characters correctly: https://stackoverflow.com/a/15262019/58173

Try "{}" first just to verify, then try "{"""msg""":"""hello"""}"

@cadeath
Copy link
Author

cadeath commented Sep 1, 2019

Thank you for the reply...

No luck

docker run --rm -v %cd%:/var/task lambci/lambda:python3.6 test.request """{"""hello""":"""world"""}"""

docker run --rm -v %cd%:/var/task lambci/lambda:python3.6 test.request """"{""""hello"""":""""world""""}""""

Output
json.decoder.JSONDecodeError: Extra data: line 1 column 4 (char 3)

@cadeath
Copy link
Author

cadeath commented Sep 1, 2019

for testing, I used {}

and the output was

START RequestId: 2648bcd9-08c6-4436-a2e5-e85bf2ff9abc Version: $LATEST
Hello!
Hmmm, does not add newlines in 3.7?


This is from Print Command
[INFO]  2019-09-01T12:55:11.388Z        2648bcd9-08c6-4436-a2e5-e85bf2ff9abc    This is INFO

END RequestId: 2648bcd9-08c6-4436-a2e5-e85bf2ff9abc
REPORT RequestId: 2648bcd9-08c6-4436-a2e5-e85bf2ff9abc Duration: 10 ms Billed Duration: 100 ms Memory Size: 1536 MB Max Memory Used: 19 MB

"done"

@mhart
Copy link
Member

mhart commented Sep 23, 2019

Hi @cadeath did you figure out how to escape quotes correctly in the end?

@mhart
Copy link
Member

mhart commented Oct 6, 2019

Closing due to lack of activity – this is just an issue with ppl figuring out command line usage in Windows, nothing to do with this repository.

@mhart mhart closed this as completed Oct 6, 2019
@cadeath
Copy link
Author

cadeath commented Mar 8, 2020

Hello,

Sorry for being in active for a while, I just tried an alternative.
However, this matter was solved.

"{"""some""":"""event"""}"`

@abasu0713
Copy link

abasu0713 commented Feb 27, 2022

I had the same trouble..

Try this:
'{"msg":"hello"}'

@dsivakumar
Copy link

I was tryin use this https://github.com/philschmid/serverless-bert-huggingface-aws-lambda-docker, got same error.
example {"body": {''context":"some blah blah", "question": "some question?"}}
uses body = json.loads(event['body']) to load
using context.log(), able to figure out the error logged this error TypeError('the JSON object must be str, bytes or bytearray, not dict').
The data already goes as a dict, so it fails on json.loads(). changed the code to

body = event['body']

works fine!
So, using context.log() and checking on docker logs screen helped!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants