Terser: How to Minify JSON

Created on 26 Dec 2019  路  6Comments  路  Source: terser/terser

From the command line, the ReadMe indicates that you can set --parse to expression in order to:

parse a single expression, rather than a program (for parsing json)

So if I have the following two files

statement.js

var object = {"a" : "b"}

expression.json

{"a" : "b"}

Then I should be able to run the following from the command line:

$ terser statement.js
// var object={a:"b"};
$ terser expression.json -p expression
// {a:"b"}

However, the same does not seem to work for the node.js API...

So parsing a JS statement works fine:

const Terser = require("terser");

Terser.minify(`var object = {"a" : "b"}`)
// { code: "var object={a:"b"};"}

But looking at the minify options, the parse option doesn't contain an expression option and the compress options do, but it's intended to:

Pass true to preserve completion values from terminal statements without return, e.g. in bookmarklets.

So neither of the following work to pass in options

const Terser = require("terser");

Terser.minify(`{"a" : "b"}`, { compress: {expression: true}}) // Error: Unexpected token: punc (:)
Terser.minify(`{"a" : "b"}`, { parse: {expression: true}})    // TypeError: e.resolve_defines is not a function

And also, to be valid JSON, the object property would still need to retain the quotes around it.

Since the minification possible for JSON is really just removing whitespace, using JSON.parse and JSON.stringify should work just fine:

JSON.stringify(JSON.parse(`{"a" : "b"}`))

Just wanted to make sure I wasn't missing anything obvious

enhancement

Most helpful comment

I agree JSON is very important. I just don't see why it's in scope for Terser to implement a JSON-to-JSON minifier, when it can JSON-to-ES which is smaller.

All 6 comments

No, you're not missing anything obvious. It should be possible to use parse: "expression" in the node API as well. Thanks for the report!

Regarding minifying JSON (with a JSON output), as you say, everything that needs to be done is parsing and then stringifying. JSON.stringify produces no whitespace between tokens, therefore it's perfectly compact.

In node and the browser you can do what you specify in your last code example, and in the CLI you can use jq . -c if you have that amazing tool installed.

@fabiosantoscode So what is official way to compress JSON? Or we need to send a PR?

If the output is JSON, Terser is not the tool (you probably know this @evilebottnawi but I'm pointing out for future googlers). You can simply do JSON.stringify(JSON.parse(input)).

If the output is JavaScript this issue needs to be solved.

A good workaround is to wrap the input code in parentheses and use the expression compress option.

If the output is JSON, Terser is not the tool (you probably know this @evilebottnawi but I'm pointing out for future googlers)

I know, but maybe we can implement it as enhancement, JSON is the popular format, a lot of projects use it for i18n/l10n/data/metadata/etc, will be great to avoid unnecessary tool only for JSON.stringify(JSON.parse(input))

I agree JSON is very important. I just don't see why it's in scope for Terser to implement a JSON-to-JSON minifier, when it can JSON-to-ES which is smaller.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sodatea picture sodatea  路  5Comments

marcusdarmstrong picture marcusdarmstrong  路  3Comments

timocov picture timocov  路  3Comments

rodrigograca31 picture rodrigograca31  路  6Comments

fedetango picture fedetango  路  3Comments