Mike Buss Mike Buss - 1 month ago 5
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')
f.write(jsonOutput)
host.upload('C:/Comparison.txt', '/public_html/Comparison.txt')
f.close()
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
http://www.digikey.com/product-detail/en/A000073/1050-10
when the full URL is actually
http://www.digikey.com/product-detail/en/A000073/1050-1041-ND/3476357
(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
f.write(re.escape(jsonOutput)
with the same results.

Can anyone help with this?

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