python - Trying to parse some json but i am getting a key error - Django -


i have request sending api , getting json format response. want grab items response being sent not getting out of it. getting error keys putting in grab.

from response below, want grab values specific keys within response , save them in variables save them within database.

here code have:

def createusersynapse(request):     argss = {         'email': 'hello@synapsepay.com',         'phone_number': '555-555-5555',         'legal_name': 'hello mchello',         'note': ':)',  # optional         'supp_id': '123abc',  # optional         'is_business': true,         'cip_tag': 1     }     user = synapseuser.create(clients, **argss)     print(user.json)     response = json.loads(user)     if response:         _id = response['_id']         name = response.client['name']         link = response._links.self['href']         cip = response.extra['cip_tag']         supp = response.extra['supp_id']         print(name)         print(_id)         print(link)         print(cip)         print(supp) 

here sample of reply:

{      '_id':'..4e57',    '_links':{         'self':{            'href':'https://uat-api.synapsefi.com/v3.1/users/..54e57'       }    },    'client':{         'id':'..26a34',       'name':'charlie brown llc'    },    'doc_status':{         'physical_doc':'missing|invalid',       'virtual_doc':'missing|invalid'    },    'documents':[       ],    'emails':[       ],    'extra':{         'cip_tag':1,       'date_joined':1504774195147,       'extra_security':false,       'is_business':true,       'last_updated':1504774195147,       'public_note':none,       'supp_id':'123abc'    },    'is_hidden':false,    'legal_names':[         'hello mchello'    ],    'logins':[         {            'email':'hello@synapsepay.com',          'scope':'read_and_write'       }    ],    'permission':'unverified',    'phone_numbers':[         '555-555-5555'    ],    'photos':[       ],    'refresh_token':'refresh_..g8lpqf6' } 

and here error getting browser:

typeerror @ /setup_profile/ json object must str, bytes or bytearray, not 'user' request method: post request url:    http://127.0.0.1:8000/setup_profile/ django version: 1.8.6 exception type: typeerror exception value:     json object must str, bytes or bytearray, not 'user' 

** update **

the following response when print user:

<class 'synapse_pay_rest.models.users.user.user'>({'client': <class 'synapse_pay_rest.client.client'>(base_url=https://uat-api.synapsefi.com/v3.1), 'json': {'_id': '..920e6', '_links': {'self': {'href': 'https://uat-api.synapsefi.com/v3.1/users/..920e6'}}, 'client': {'id': '..026a34', 'name': 'charlie brown llc'}, 'doc_status': {'physical_doc': 'missing|invalid', 'virtual_doc': 'missing|invalid'}, 'documents': [], 'emails': [], 'extra': {'cip_tag': 1, 'date_joined': 1505093840940, 'extra_security': false, 'is_business': true, 'last_updated': 1505093840940, 'public_note': none, 'supp_id': '123abc'}, 'is_hidden': false, 'legal_names': ['hello mchello'], 'logins': [{'email': 'hello@synapsepay.com', 'scope': 'read_and_write'}], 'permission': 'unverified', 'phone_numbers': ['555-555-5555'], 'photos': [], 'refresh_token': 'refresh_..cybeirna3p'}, 'id': '..920e6', 'refresh_token': 'refresh_..eirna3p', 'logins': [{'email': 'hello@synapsepay.com', 'scope': 'read_and_write'}], 'phone_numbers': ['555-555-5555'], 'legal_names': ['hello mchello'], 'permission': 'unverified', 'note': none, 'supp_id': '123abc', 'is_business': true, 'cip_tag': 1, 'base_documents': 0, 'oauth_key': 'oauth_..dqzuj', 'expires_in': '7200'}) 

here comes type user , type user.json:

type(user) returns

<class 'synapse_pay_rest.models.users.user.user'> 

type(user.json)

<class 'dict'> 

the error coming json loads line

response = json.loads(user) 

this because json.loads expects string object can converted json dictionary, being passed user object, , consequently throwing typeerror.

in case, since type of user.json dictionary, can replace response line this:

response = user.json 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -