Answer the question
In order to leave comments, you need to log in
How to add elements to json files (python)?
I take data from API and create json file
with open("file1.json", "w") as file:
for t in x:
response = requests.get(url=f'...={t}')
json.dump(response.json(), file, indent=4, ensure_ascii=False)
{
"code": 200,
"data": [
{
"key1": "KARA",
"key2": "13"
}
]
}{
"code": 200,
"data": [
{
"key1": "CRACK",
"key2": "4"
}
]
}
[
{
"code": 200,
"data": [
{
"key1": "KARA",
"key2": "13"
}
]
},
{
"code": 200,
"data": [
{
"key1": "CRACK",
"key2": "4"
}
]
}
]
Answer the question
In order to leave comments, you need to log in
Obviously, you need to form a list of dictionaries and save it once, and not save the dictionary on each iteration.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question