I am converting a JSON file (from an ajax call) into CSV.
When the JSON file is sent to me, it is 80kb.
When I save the contents of the JSON file into a .txt file, it becomes 291kb!
After converting the .txt file into a .csv file, it's 240kb.
How is the JSON file I received from an ajax call so much smaller a .txt file I created with identical content? Is there some way to reduce the size of the end product?
EDIT:
This is how I am getting the file size.
EDIT:
I don't think the .txt to .csv conversion problem is the issue, but here is my code:
import json
import csv
import re
with open('jjj.txt') as f:
f = f.read()
parsed = json.loads(f)
unix_time = re.compile(r'(\d\d\d\d\d\d\d\d\d\d\d\d\d)')
data = parsed['d']['tables'][0]['rows']
for i in data:
for a in range(len(i)):
if a > 39 and a < 46:
if i[a] != None:
mo = unix_time.search(i[a])
i[a] = mo.group(1)
file = open('json.csv', 'w', newline='')
csvwriter = csv.writer(file)
csvwriter.writerows(data)
JSON is a string format used mostly for communication. If we want to save the JSON string in a file, it will be a text file. In this case, there will be no difference between JSON or any other content of the text file.
You are receiving a JSON string from your Ajax call, not a JSON file. You are receiving it over HTTP, and it is compressed (g-zipped). So you are comparing the size of the compressed text with the flat one which you are creating. Zip the file you are creating and you will get it down to almost the same size (depending on the compression tool and settings).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With