5

If you try to send an object that's not JSON serializable (anything other than list, dictionary, integer, etc.), you get the following error message:

"errorMessage": "Object of type set is not JSON serializable" 

Even if you send a tuple, it gets converted into a list when it's passed to the second lambda.

So, what are the workarounds, if you want to preserve the object?

1 Answer 1

5

One option:

  • Create an encoded string representation of the object and pass it, using a function like this:

    def native_object_encoded(x): x = pickle.dumps(x) x = zlib.compress(x) x = base64.b64encode(x).decode() return x 
  • In the second lambda function, perform the same steps in reverse to get back the original object:

    def native_object_decoded(s): s = base64.b64decode(s) s = zlib.decompress(s) s = pickle.loads(s) return s 

Usage:

# lambda 1 (the caller) a = {66, 99, 35} # set: not serializable payload = { 'a': native_object_encoded(a), } response = client.invoke( ... ) # lambda 2 (the one being invoked) a = native_object_decoded(event['a']) 

Notes:

  • If the object is of custom type, that type must be defined in the second lambda function too.
  • If the object is part of some other library (standard or third-party), that library should be imported in the second lambda function.
  • The compression step is optional.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.