Apr-01-2022, 11:01 PM
I have an api that returns me a json of 5 out of 5 data, for example. In the example I have an output with 9 data. And in the json it gives me a
I have the following output:
In the output of the file, instead of leaving the token at least once (I changed it for example), it is leaving 4 times and does not add to the parameter.
The json only exits with 5 and the
In larger json, I have to do it manually and it's tedious.
nextBatchToken token which when added to the parameter returns me the next page of the json. I want to save the final.json with the 9 data at once.I have the following output:
"data": {
"totalCount": 9,
"detections": [
{
"source": "detection",
"uuid": "6122200f",
"detectionTime": "2022-03-27T01:32:41Z",
"ingestionTime": "2022-03-27T01:45:34Z",
"filters": [...]
},
"nextBatchToken": "TOKEN"
}
}My code:import requests, json
import datetime, time
archive = open("final.json", "w")
with archive as finaljson:
date_time_start = datetime.datetime(2022, 3, 26, 21, 00, 00)
date_time_end = datetime.datetime(2022, 3, 27, 9, 00, 00)
start = int((time.mktime(date_time_start.timetuple())))
end = int((time.mktime(date_time_end.timetuple())))
# response = json.dumps(r.text)
url_base = 'URL'
url_path = '/PATH'
token = 'MY_YOKEN'
nextBatchToken = ''
query_params = {
'nextBatchToken': nextBatchToken,
'end': end,
'size': '5',
'start': start,
'riskLevels':'critical'
}
headers = {'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json;charset=utf-8'}
response = requests.get(url_base + url_path, params=query_params, headers=headers)
# archive = response.text
decodedResponse = json.loads(response.text)
# decodedResponse = json.dumps(response.json())
r = decodedResponse['data']
count = r['totalCount']
json.dump(response.json(), archive)
for i in r:
nextBatchToken = r['nextBatchToken']
if 'nextBatchToken' in r:
print('nextBatchToken') #I add the variable here, oculting
nextBatchToken = r['nextBatchToken']
else:
print('cabo')
archive.close()I tried to make an increment shape but I can't send the json response to the variable and print the next page on this for but no success...In the output of the file, instead of leaving the token at least once (I changed it for example), it is leaving 4 times and does not add to the parameter.
The json only exits with 5 and the
nextBachToken included.Output:C:\Users\Sandson\Desktop
λ python vai.py
MY_TOKEN
MY_TOKEN
MY_TOKEN
MY_TOKENThe idea is to take the skiptoken, access the next page of the json and save it in csv.In larger json, I have to do it manually and it's tedious.
