How To Add An Element To An Empty JSON In Python?
I created an empty string & convert it into a JSON by json.dump. Once I want to add element, it fails & show AttributeError: 'str' object has no attribute 'append' I trie
Solution 1:
JSON is a string representation of data, such as lists and dictionaries. You don't append to the JSON, you append to the original data and then dump it.
Also, you don't use append()
with dictionaries, it's used with lists.
data = {} # This is a dictionary
data["a"] = "1"; # Add an item to the dictionary
json_data = json.dumps(data) # Convert the dictionary to a JSON string
print(json_data)
Post a Comment for "How To Add An Element To An Empty JSON In Python?"