Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a JSON file so much smaller than a .txt file with the same content?

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.

  1. I find the AJAX request, and check its file size. Link. As you can see, it is about 80kb.
  2. I copy the source of the request. Link.
  3. Then I copy and paste the source into a blank .txt file. The result is a .txt file that is 291 kb in 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)
like image 286
Valachio Avatar asked Nov 16 '25 23:11

Valachio


1 Answers

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).

like image 79
Racil Hilan Avatar answered Nov 18 '25 20:11

Racil Hilan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!