Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compress JSON file by eliminating whitespace

I am working with a large json file (~100,000 lines) and need to compress it down to make a program run faster. I wish to delete all the horizontal tabs, returns, etc. to minimize the size of the file.

For example if a line was originally:

"name_id": "Richard Feynman",
"occupation": "Professional Bongos Player"

it should be compressed to:

"name_id":"Richard Feynman","occupation":"Professional Bongos Player"`

I have scoured the Internet (forgive me if it is a simple answer, I am a beginner) and can't seem to find a command for the terminal that will help me do this. Any help would be much appreciated

like image 549
ibanez221 Avatar asked Aug 12 '14 00:08

ibanez221


People also ask

How do you remove spaces from a JSON file?

Use underscores “_” between the word of the JSON Object key instead of whitespace.

Does JSON Stringify remove white spaces?

JSON. stringify(body) returns it without spaces. Did you try to see if there are spaces before trying to remove it from there?

Can you compress a JSON file?

As text data, JSON data compresses nicely. That's why gzip is our first option to reduce the JSON data size. Moreover, it can be automatically applied in HTTP, the common protocol for sending and receiving JSON. Let's take the JSON produced with the default Jackson options and compress it with gzip.

How do you minify JSON in Python?

The default is (', ', ': ') if indent is None and (',', ': ') otherwise. To get the most compact JSON representation, you should specify (',', ':') to eliminate whitespace.


2 Answers

Looks like you're looking for a JSON minifier.

There are some around, both online and standalone.

Try googling these terms + your favorite language, I'm sure you'll find something that suits your needs.

There are other tools that modify your JSON to make it smaller, but you'll end up with a different JSON, I guess. Haven't tried those.

like image 151
Leo Avatar answered Oct 20 '22 00:10

Leo


Using GNU awk for RT:

$ awk 'BEGIN{RS="\""} NR%2{gsub(/[[:space:]]/,"")} {ORS=RT;print} END{printf "\n"}' file
"name_id":"Richard Feynman","occupation":"Professional Bongos Player"
like image 22
Ed Morton Avatar answered Oct 19 '22 23:10

Ed Morton