0
votes

My use case is to take a JSON message from SQS body and insert data into DynamoDB Using the lambda function in python. the issue is I am able to read and print the JSON message from SQS queue into cloud watch log but when I try to insert the same JSON in dynamoDB it gives below Error

Invalid type for parameter Item, value: {'name': 2}, type: class 'str', valid types: class 'dict'

Below is the lambda code I am using and an error occurred at line number 12 where I am trying to insert using put_item

import json
import boto3

dynamodb = boto3.resource('dynamodb')
dynamoTable = dynamodb.Table('message')

def lambda_handler(event, context):
    for record in event['Records']:
        data1 = record["body"]
        jsondata1 = json.loads(data1)
        print(jsondata1)
        dynamoTable.put_item(Item=jsondata1)      

However, it is able to print the SQS JSON to cloud watch log as below enter image description here

1
You will get this error when jsondata1 from your example will be of a string type. Is this the actual code that is giving you the error? - Tommy
I am able to print jsondata1 in the console but it's giving an error while inserting into dynamoDB i.e last line of code dynamoTable.put_item(Item=jsondata1) - Narendra Maru
I was trying to recreate the error and accordingly to the error message you are getting, you will get that error only when jsondata1 is of type string. Where in your code snippet it isn't. Hence my question is if the sample code you provided is the actual code that is giving you an error. - Tommy
what happens is dynamoDB expects data in type dictionary but when I print type of jsondata1 its giving string hence actually it should be dict, in conclusion the thing is AWS lambda is not able to convert SQS message to dict data type - Narendra Maru
it will be great if u can help me with any other way in python lambda or boto 3 to convert string to dict class - Narendra Maru

1 Answers

0
votes

after so many R&D i am able to find the solution is by splitting the string by comma and then recreating an json which will create json of dict data type and not string

Below is the code for the same solution

import json
import boto3
import ast

dynamodb = boto3.resource('dynamodb')
dynamoTable = dynamodb.Table('message')

def lambda_handler(event, context):
    for record in event['Records']:
        data1 = record["body"]
        jsondata1 = json.loads(data1)
        mess1 = jsondata1["Message"]
        id = jsondata1["MessageId"]
        jsonmess = json.loads(mess1)
        s = jsonmess.replace("{" ,"")
        finalstring = s.replace("}" , "")
        split = finalstring.split(",")
        dict = {'messageID':id}
        for x in split:
            keyvalue = x.split(":")
            print(keyvalue)
            dict[keyvalue[0]]=keyvalue[1]
        dynamoTable.put_item(Item=dict)