Alankar Gupta
Alankar Gupta

Reputation: 201

Not able to save JSON file in Python

Can someone help me in pointing out whats wrong with the below function?

def RetrieveQuotes(token, appid):
 quoteRequestMsg = json.load(open('GetEventHeadlines_Request_1.json'))

 quoteURL = 'http://api.trkd.thomsonreuters.com/api/StreetEvents/StreetEvents.svc/REST/StreetEvents_2/GetEventHeadlines_1'
 headers = {'content-type': 'application/json;charset=utf-8',
           'X-Trkd-Auth-ApplicationID': appid, 'X-Trkd-Auth-Token': token}

 print('############### Sending Quote request message to TRKD ###############')
 quoteResult = doSendRequest(quoteURL, quoteRequestMsg, headers)
 if quoteResult and quoteResult.status_code == 200:
    print('Quote response message: ')
    #print(quoteResult.json())
    #print(json.dumps(quoteResult.json(), sort_keys=True,indent=2, separators=(',', ':')))
 with open('quoteResult.json', 'w') as f:
    json.dump(quoteResult, f)

When i am printing the JSON file its working fine but i am not able to save the contents of the JSON to my local. I am getting the below error.

Object of type 'Response' is not JSON serializable

Can someone help me on this?

Upvotes: 0

Views: 265

Answers (2)

Kalpit
Kalpit

Reputation: 1101

You'll have to use quoteResult.text to get the raw text from the response that you can pass to json.dump()

Upvotes: 1

Vikas Sharma
Vikas Sharma

Reputation: 461

quoteResult is response code use quoteResult.content to save

Upvotes: 0

Related Questions