0

I want to create a python code using with open such that the start and end of the json file has square brackets i.e [] and the content inside which is in the form of a number of dictionaries must be seperated by commas.

I have a json file like this

{
    "Day": "Monday",
    "Month": "May",
    "Name": "Hello"
}
{
    "Name": "Hello",
    "Day": "Monday",
    "Month": "May"
}

I want it to be in this form

[{
    "Day": "Monday",
    "Month": "May",
    "Name": "Hello"
},
{
    "Name": "Hello",
    "Day": "Monday",
    "Month": "May"
}]

I am able to generate the file in the above way but I want to generate the file as shown in the below figure. Thank you.

import requests
import json
import csv


with open('file.csv') as intfile:    

        payload = ""
        
        
        response = requests.request("GET",  data=payload)
        temp = json.loads(response.text)
        
        with open('data.json','a') as fs:
            json.dump(temp,fs, indent=4,sort_keys=True)

        #print(type(response.text))
with open('data.json') as json_file:
    jsondata = json.load(json_file)

data_file = open('jsonoutput.csv', 'w', newline='')
csv_writer = csv.writer(data_file)
print(csv_writer)
 
count = 0
for data in jsondata:
    if count == 0:
        header = data.keys()
        csv_writer.writerow(header)
        count += 10
    csv_writer.writerow(data.values())
 
data_file.close()
Ateeq
  • 5
  • 4

0 Answers0