0

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]}]

martineau
  • 119,623
  • 25
  • 170
  • 301
Matt
  • 27
  • 7
  • 1
    Does this answer your question? [Parsing string as JSON with single quotes?](https://stackoverflow.com/questions/36038454/parsing-string-as-json-with-single-quotes) – Kristian Jul 03 '21 at 17:00
  • 4
    your `response` is not a json, it is python `dict`, to convert it into json do `json.dumps(response)` after `import json` or instead of json use `requests.get(url=URL, params=PARAMS).text`. Simply put `with open('you_json_file_name.json', 'w') as f: f.write(requests.get(url=URL, params=PARAMS).text)` – Epsi95 Jul 03 '21 at 17:03
  • 1
    Did you mean ‘return response.json()’? – quamrana Jul 03 '21 at 17:05
  • @Epsi95 Yup that solved everything. – Matt Jul 03 '21 at 17:11
  • 1
    @Matt you don't need to do any replacing of `'` or `"`, `requests.get(url=URL, params=PARAMS).text` is already a json, check by `requests.get(url=URL, params=PARAMS).headers['Content-Type']`, It should be `'application/json; charset=utf-8'`. just save it to a file, that's it. – Epsi95 Jul 03 '21 at 17:21

1 Answers1

0

Thank you @Epsi95

def getInfo(type, debug=False):
    PARAMS = {
        'token': '',
        'type': type}
    URL = urljoin(host, '/data')
    response = requests.get(url=URL, params=PARAMS).text.replace("'", '"')
    
    if debug is True:
        mkdir('json')
        jsonFile = open(os.path.join('json', type + ".json"),
                        "w", encoding='utf-8').write(str(response))
    return json.loads(response)```
Matt
  • 27
  • 7