Taariq Toffar
Taariq Toffar

Reputation: 49

Adding API requests into one JSON using Python

I just started working with Python and API's and wanted to know if there is a way to accomplish the below, the API I'm using has parameters for from and to date, I however need the monthly breakdowns

apilink/getKwh?token={token}&clientId={clientId}&siteId={siteId}&from={from}&to={to}

{
    "data_json": [
        {
            "site": "Test Western Cape DC",
            "year": 2021,
            "month": "2021-01-01 - 2021-01-31",
            "total_kWh": {
                "grid_electricity": 708550.8319999998,
                "pv_electricity": 191839.79899999997
            }
        }
    ]
}

So im using the below Python code to dump into JSON:

from urllib.request import urlopen
import json
url = "apilink/getKwh?token={token}&clientId={clientId}&siteId={siteId}&from=2021-01-01&to=2021-01-31"
response = urlopen(url)
data = json.loads(response.read())

jsonString = json.dumps(data,data2)
jsonFile = open("data.json", "w")
jsonFile.write(jsonString)
jsonFile.close()

How would I go about adding additional months to the JSON file I'm dumping to?

Upvotes: 0

Views: 74

Answers (3)

Sabil
Sabil

Reputation: 4510

You should have to do something like this:

It will generate monthly JSON file using API response. If you want data in single file then let me know.
It will generate a JSON file like this name data-2021-01.json for each month

Code for Single JSON File:

import calendar
from urllib.request import urlopen
import json

year = 2021
json_data = []

for month in range(1, 13):
    r = calendar.monthrange(year, month)
    start = f"{year}-{month:0>2d}-01"
    end = f"{year}-{month:0>2d}-{r[1]}"
    
    url = f"apilink/getKwh?token={token}&clientId={clientId}&siteId={siteId}&from={start}&to={end}"
    response = urlopen(url)
    data = json.loads(response.read())
    json_data.append(data)

with open('data.json', 'w') as file:
    json.dump(json_data,file, indent=4)

Code for Every Month's JSON

import calendar
from urllib.request import urlopen
import json

year = 2021
json_data = []

for month in range(1, 13):
    r = calendar.monthrange(year, month)
    start = f"{year}-{month:0>2d}-01"
    end = f"{year}-{month:0>2d}-{r[1]}"
    filename = f'data-{year}-{month:0>2d}.json'
    
    url = f"apilink/getKwh?token={token}&clientId={clientId}&siteId={siteId}&from={start}&to={end}"
    response = urlopen(url)
    data = json.loads(response.read())

    json_data.append(data)

    with open(filename, 'w') as file:
        json.dump(data, file, indent=4)

Upvotes: 1

balderman
balderman

Reputation: 23815

prepare the dates range and loop over it

from urllib.request import urlopen
import json

#
# TODO - replace with real values
#
dates = [{'from': 1, 'to': 1}, {'from': 2, 'to': 2}]
token = 't--replace'
clientId = 'c--replace'
siteId = 's--replace'
for entry in dates:
    url = f"apilink/getKwh?token={token}&clientId={clientId}&siteId={siteId}&from={entry['from']}&to=entry['to']"
    response = urlopen(url)
    data = json.loads(response.read())

Upvotes: 0

Trock
Trock

Reputation: 636

Can use calendar:

import calendar

year = 2021

for month in range(1, 13):
    r = calendar.monthrange(year, month)
    start = f"{year}-{month:0>2d}-01"
    end = f"{year}-{month:0>2d}-{r[1]}"
    filename = f'data-{year}-{month:0>2d}.json'
    
    url = f"apilink/getKwh?token={token}&clientId={clientId}&siteId={siteId}&from={start}&to={end}"
    ...

Upvotes: 0

Related Questions