Apr-02-2020, 04:57 PM
I am trying to add key\value pairs to a Json file. I can write one pair to the file but I can't figure out how to append additional pairs. When I try, it just replaces the current pair. I have tried following this tutorial but I get key errors.
import json product_data = "{}" product = input("Enter product name. ") price = input("enter product price. ") new_product = {product:price} load = json.loads(product_data) load.update(new_product) with open("Product.json", "r+") as file: json.dump(load, file)console output:
Output:Enter product name. milk
enter product price. 2.99
File output:Output:{"milk": "2.99"}