-1

How can I change the ' ' to " " ? I need to write database.json using the data from sqlalchemy

Data is the name of my database

all_data = Data.query.all()
data_schema = DataSchema(many=True)
output = data_schema.dump(all_data) 
OutputJson = jsonify({'products':output})

while True:
    with open('database.json',"w") as file:
        file.write(str({'products': output}))
    
    with open('database.json',"r") as files:
        FinalOutput = files.read()

        return FinalOutput

But my output is this:

{'products': [
    {'id': 0,
     'name': 0,
     'price': 0,
     'quantity': 1
    }
]}

and it should be like that:

{
  "products": [
    {
      "id": 1,
      "name": "Aspirina",
      "price": 100,
      "quantity": 1
    }
  ]
}

1 Answers1

0

Modify this part:

with open('database.json',"w") as file:
        file.write(str({'products': output}))

as

import json
json_data = {}
with open('database.json',"r") as file:
        json_data = json.load(file)
json_data['products'] = output
with open('database.json',"w+") as file:
        json.dump(json_data)