I have a code that needs to read a JSON file with multiple lines, i.e:
{"c1-line1": "value", "c2-line1": "value"}
{"c1-line2": "value", "c2-line2": "value"}...
and, after change the keys values (already working), I need to write a new json file with these multiple lines, i.e:
{"newc1-line1": "value", "newc2-line1": "value"}
{"newc1-line2": "value", "newc2-line2": "value"}...
My problem is that my code are just writing the last value readed:
{"newc1-line2": "value", "newc2-line2": "value"}
My code:
def main():
... # changeKeyValueCode
writeFile(data)
def writeFile(data):
with open('new_file.json', 'w') as f:
json.dump(data, f)
I already tried with json.dumps
and just f.write('')
or f.write('\n')
I know that data in writeFile()
is correctly with each line value.
How can I resolve this, please?
open file with (a), it will search the file if found append data to the end, else it will create empty file and then append data.