masterofdestiny
masterofdestiny

Reputation: 2811

Not able to Convert JSON to Python object

I am getting a JSON response from an API like this:

{"code": 200, 
"data": {"messageKey": {}, "returncode": "SUCCESS",
    "meetingID": "REG_561538474", "hasBeenForciblyEnded": "false", 
    "moderatorPW": "58963", "attendeePW": "54321", "message": {},
    "createTime": "1364280051568"}, "errors": null}

Now I want to convert this into the Python object and save some value in a database.

Here is my code:

getmobject  = Meeting()
getj = json.loads(r.text)
for x,v in getj.items():
   if x == 'data':
      if x.meetingID:# == 'meetingID':
         getmobject.meetingID = x.meetingID
         if x.moderatorPW:
            getmobject.moderator_passwd = x.moderatorPW
            if x.attendeePW:
               getmobject.attendee_passwd = x.attendeePW
               getuser_main = User.objects.get(username = request.user.username)
               getmobject.name = get_meeting_name
               getmobject.created_by = getuser_main
               getmobject.sms_no = '12345'

               """Reminder we are making false as if participant accept then only
                  meeting will able to initiate"""
               getmobject.reminder = False   
               getmobject.save()

But problem is I am getting the error:

'unicode' object has no attribute 'meetingID'

Please help me out what might I am doing wrong here

Upvotes: 0

Views: 129

Answers (1)

MostafaR
MostafaR

Reputation: 3695

In your code when x == "data", then v is the dictionary appeared in front of "data" in the json string, so you should use v["meetingID"] instead of x.meetingID and so on ...

Upvotes: 1

Related Questions