Search code examples
pythoncherrypy

Iterate through list passed as **kwargs in cheerypy python


Hi I am new to cheerypy and python

I am passing list of objects

EX:

deviceArr=[{"id":"01","name":"abc"},{"id":"02","name":"abcd"}]

to the Post request

      def POST(self,*args, **kwargs):
            abc=[]
            abc=kwargs['deviceArr']
            print abc #output -[{"id":"01","name":"abc"},{"id":"02","name":"abcd"}]
            for device in abc:
                print device #its printing [,{,",i,d...

i want it to print complete object on Iteration

    {"id":"01","name":"abc"}
    {"id":"02","name":"abcd"}

so can any one please help me how to achieve it. Thanks


Solution

  • The post data is being received as a JSON object which is a string. You need to convert it to a Python object first, in order to iterate over your data as a list of dictionaries and not as a string:

    import json
    
    ...
    abc = kwargs['deviceArr']
    abc = json.loads(abc)
    ...