Mike Buss Mike Buss - 10 months ago 43
JSON Question

Can't write long JSON output to text file

I have a long string (8,315 characters) worth of JSON, but I can't seem to write it to a .txt file using Python without it being truncated.

I write the JSON to a text file and then upload it via FTP, but both the .txt file on my system and the .txt file on the FTP server are truncated.

Here's the code:

# Upload the results
host = ftputil.FTPHost('ftp.website.com', 'username', 'password')
jsonOutput = json.dumps(full_json)
f = open('C:/Comparison.txt', 'w')
host.upload('C:/Comparison.txt', '/public_html/Comparison.txt')
print jsonOutput

The JSON output in the console is valid and whole, but it is truncated in the .txt file that is written (and then the .txt file after it is uploaded).

Most of the time, the output will end at
when the full URL is actually
(and then of course, it cuts off the rest of the JSON)

I'm not sure if this makes any difference, but I also tried
with the same results.

Can anyone help with this?

Answer Source
with open('C:/Comparison.txt', 'w') as f:
  json.dump(full_json, f)