Home > python > opening gzipped JSON files

opening gzipped JSON files

I have a project where the input JSON file is almost 7 MB. I keep this project in my Dropbox folder, so that 7 MB text file seems to be a waste. Any way to reduce its size?

I zipped it up with gzip: “gzip -9 input.json“. This command produced a 1.3 MB “input.json.gz” file and deleted the original. Good. But how to open it in Python?

Normal way (without gzip):

import json

with open("input.json") as f:
    d = json.load(f)

Compressed way (with gzip):

import json
import gzip

with gzip.open("input.json.gz", "rb") as f:
    d = json.loads(f.read().decode("ascii"))

I didn’t notice any performance penalty. The application that first reads this json file starts as fast as before.

Categories: python Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: