Home > other >  How to convert a python dict to json in order to submit to kinesis
How to convert a python dict to json in order to submit to kinesis

Time:08-05

So I am trying to push some data into kinesis stream using Python and the record needs to be in JSON format but for the life of me I cant seem to get it correct.

I have a python dict object:

o = {'Name':'John Doe'}

I want to submit this to kinesis, so I did the following:

//omitting kinesis initialisation and all..
response = kinesis_client.put_records(
   StreamName='kinesis_test_stream',
   Records=json.dumps(o)
)

It gives me following error:

type: <class 'str'>, valid types: <class 'list'>, <class 'tuple'>

So instead of json.dumps(o) I tried Records=json.loads(json.dumps(o))

Then it gives me this error:

type: <class 'dict'>, valid types: <class 'list'>, <class 'tuple'>

I have been banging my head around this for an hour now, I think it should be something very small and silly that I am missing.

I tried this SO post but nothing helped.

Can anyone help me please?

CodePudding user response:

As the error suggests, the Records parameter expects a list. Try

response = kinesis_client.put_records(
   StreamName='kinesis_test_stream',
   Records=[o]
)

And check the docs for put_records.

  • Related