I'm writing a function that uses requests.get to acquire a json raw file, writing it to a file, converting all the single qutoes to double quotes (the api outputs a json file with singlequotes around keys), writing it to the file, then returns the data. The one I wrote simply doesn't work, and if it does, it's still very sloppy which writes and reads a physical file twice. Are there potential ways to make it more efficient?
def getInfo(type):
PARAMS = {
'token': '',
'type': type, }
URL = urljoin(host, '/data')
response = requests.get(url=URL, params=PARAMS).json()
try:
os.mkdir('json')
except:
pass
jsonFile = open(os.path.join('json', type + ".json"),
"w").write(str(response))
filer = open(os.path.join('json', type + '.json'), 'r')
file = open(os.path.join('json', type + '.json'), 'w')
for line in filer:
# read replace the string and write to output file
file.write(line.replace("'", '"'))
file.close()
jsonFile = open(os.path.join('json', type + ".json"),
"w").write(str(file))
with open(os.path.join('json', type + ".json")) as f:
data = json.loads(f.read())
return data
example.json
[{'api_member_id': 14171432, 'api_id': 1, 'api_name': 'No.1', 'api_name_id': '', 'api_mission': [0, 0, 0, 0], 'api_flagship': '0', 'api_ship': [341, 38, 317, 345, 145, 50]}, {'api_member_id': 14171432, 'api_id': 2, 'api_name': 'No.2', 'api_name_id': '', 'api_mission': [0, 0, 0, 0], 'api_flagship': '0', 'api_ship': [13, 19, 118, 36, -1, -1]}, {'api_member_id': 14171432, 'api_id': 3, 'api_name': 'No.3', 'api_name_id': '', 'api_mission': [0, 0, 0, 0], 'api_flagship': '0', 'api_ship': [58, 167, 14, 146, -1, -1]}, {'api_member_id': 14171432, 'api_id': 4, 'api_name': 'No.4', 'api_name_id': '', 'api_mission': [0, 0, 0, 0], 'api_flagship': '0', 'api_ship': [-1, -1, -1, -1, -1, -1]}]