Write JSON to a file without writing escape backslashes to file?

Viewed 2568

I am trying to write code that will write JSON to a file without including the backslashes used to escape the quotes. Currently my output looks like this:

"https://api.flickr.com/services/rest/format-json_method-flickr.photos.getInfo_photo_id-34869402493": "{\"photo\":{\"id\":\"34869402493\",\"secret\":\"56fcf0342c\",\"server\":\"4057\",\"farm\":5,\"dateuploaded\":\"1499030213\",\"isfavorite\":0,\"license\":\"0\",\"safety_level\":\"0\",\"rotation\":0,\"originalsecret\":\"c4d1d316ed\",\"originalformat\":\"jpg\",\"owner\":{\"nsid\":\"150544082@N05\",\"username\":\"ankitrana_\",\"realname\":\"Ankit Rana\",...

Here is what I am aiming for:

{"https://api.flickr.com/services/rest/format-json_method-flickr.photos.search_per_page-50_tag_mode-all_tags-mountains": {"photos": {"page": 1, "pages": 5023, "perpage": 50, "total": "251136", "photo": [{"id": "35678630595", "owner": "61623564@N06", "secret": "97e4f3a4a2", "server": "4255", "farm": 5, "title": "IMG_9105", "ispublic": 1, "isfriend": 0, "isfamily": 0}, {"id": "35678623895", "owner": "61623564@N06", "secret": "f24e2ebb04", "server": "4003", "farm": 5, "title": "IMG_9247", "ispublic": 1, "isfriend": 0, "isfamily": 0},

This is the function as I have it written at the moment:

def get_photo_data(photo_id):
para = {}
para["photo_id"] = photo_id
para["method"] = "flickr.photos.getInfo"
para["format"] = "json"
para["api_key"] = FLICKR_KEY
request_data = params_unique_combination("https://api.flickr.com/services/rest/", para)

if request_data in CACHE_DICTION:
    return CACHE_DICTION[request_data]
else:
    response = requests.get("https://api.flickr.com/services/rest/", para)
    jstring = response.text[14:-1]
    CACHE_DICTION[request_data] = jstring.replace(r'\"','"')
    cache_file = open(CACHE_FNAME, 'w')
    cache_file.write(json.dumps(CACHE_DICTION))
    cache_file.close()
    return CACHE_DICTION[request_data]

I believe the issue is introduced with the encoding done in the .dumps method because even though I am removing the \ they remain in the written file.

1 Answers
Related