[Python] How to make a list of JSON objects into a JSON file in Python and join multiple ones?...

Discussão em 'Python' iniciado por Stack, Abril 1, 2025.

  1. Stack

    Stack Membro Participativo

    I am working with an API that returns the following format:

    {
    "count": 900,
    "next": "api/?data&page=2",
    "previous": null,
    "results":
    [{json object 1}, {json object 2}, {...}]
    }


    Problem is that I want to retrieve all "results" from all pages, and save that into one json file.

    I'm thinking of a while loop that keeps making requests to the API and aggregating the resulting "results" into one variable, until the "next" value is null.

    Something like

    while json1["next"] != null:
    r = request.get(apiURL, verify=False, allow_redirects=True, headers=headers, timeout=10)
    raw_data = r.json()["results"]

    final_data.update(raw_data)


    I tried it but since r.json()["results"] is a list I don't know how to handle the different formats and transform that into a JSON file

    When trying to do final_data.update(raw_data) it gives me an error saying:

    'list' object has no attribute 'update'


    Or when trying json.loads(raw_data) it gives me:

    TypeError: the JSON object must be str, bytes, or bytearray, not list"

    Continue reading...

Compartilhe esta Página