I have a file, and each line of the file is a json object. For example:
{name:value1,city:value2}
{name:value3,city:value4}
etc.
What I am trying to do is run a line-by-line search on the file. If the value is equal to the searched parameter, update the value of the 2nd key in the object.
So for example, if the searched value is value1, I want to update the value of city for that object to an argument.
Here is what I have so far:
//trying first with 'fixed' values, will change to arguments once figured out...
with open('jsonfile.json', "r+", encoding='utf-8') as file:
for line in file:
res = json.loads(line)
test_name = res['name']
test_city = res['city']
if test_name == "1":
res['city'] = "hello"
json.dump(res, file)
The current code successfully identifies if the test_name
matches the searched parameter, however instead of updating the value, it appends a new dictionary entry at the end of the file.