I am using python, and I have a large 'outputString' that consists of several outputs, each on a new line, to look something like this:
{size:1, title:"Hello", space:0}
{size:21, title:"World", space:10}
{size:3, title:"Goodbye", space:20}
However, there is so much data that I cannot see it all in the terminal, and would like to write code that automatically writes a json file. I am having trouble getting the json to keep the separated lines. Right now, it is all one large line in the json file. I have attached some code that I have tried. I have also attached the code used to make the string that I want to convert to a json. Thank you so much!
for value in outputList:
newOutputString = json.dumps(value)
outputString += (newOutputString + "\n")
with open('data.json', 'w') as outfile:
for item in outputString.splitlines():
json.dump(item, outfile)
json.dump("\n",outfile)
indentkeyword argument ofjson.dumphelp you?outputStringa string? Are you forced to have a string here or did you decided to make it a string?json.dump(item, outfile, indent=4). But you probably do not want to be writing multiple JSON dumps to the same file in any case...indentif you want). Otherwise your output file is not valid JSON.