Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I minify JSON in a shell script?

I've been looking for a way to uglify some JSON while in my bash console. This help using it afterward in another command (for example, to pass json inline to httpie)

Giving:

{
    "foo": "lorem",
    "bar": "ipsum"
}

I want to obtain:

{"foo":"lorem","bar":"ipsum"}

NOTE: this question is intentionnaly greatly inspired by it's pretty-print counterpart. However, googling for bash minify json didn't give me a proper result, hence this questions for the minify/uglify.

like image 980
Ulysse BN Avatar asked Apr 09 '20 15:04

Ulysse BN


People also ask

How do I minify a JSON file?

Use your JSON REST URL to minify. Click on the URL button, Enter URL and Submit. Users can also minify the JSON file by uploading the file. Once you have minified JSON Data.

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.


1 Answers

You can use jq -c (compact) option.

jq -c . < input.json

like image 85
guizo Avatar answered Sep 20 '22 23:09

guizo