I have a .json file which I would like to append data to. But only if this string is not in the .json file already.
JSON File:
[{"filename":"file1"}, {"filename":"file2"}, {"filename":"file3"}]
End result JSON File:
[{"filename":"file1"}, {"filename":"file2"}, {"filename":"file3"}, {"filename":"file4"}]
I currently have this:
with open('gdrivefiles.json', 'r') as f:
filenameslist = json.load(f) #Loads the .json file into a string (If I'm right)
for distro in filenameslist:
filenames = distro["filename"] #Gets a list of all the filenames
if name in filenames:
print("yes") #If name is in the list of filenames print 'yes'
else:
print("no") #If name is in the list of filenames print 'no'
(This code is put in a for-loop, so it will run this code for every new value of name
)
How would I add name
({"filename":"name"}
) in the json file if it is not in it already?