Skip to content

Fix invalid JSON files

Last updated:

You have bunch of improperly formatted JSON files and want an automated way to fix them.

Install hjson via

Terminal window
npm i -g hjson

Lets assume we have the file:

a.json
// This file contains comments which are not allowed in JSON
[
{id: 1, moo: true}, // missing quotes
{"id": 2}, // invalid trailing commas
]

To fix just pipe it into hjson:

Terminal window
$ cat a.json | hjson -j
[
{
"id": 1,
"moo": true
},
{
"id": 2
}
]

You can then pipe the result into other CLI tools such as fx for further analysis:

Terminal window
$ cat a.json | hjson -j | fx 'map("id")'
[
1,
2
]