JSON with comments

We have a hundred json files that are currently hand-edited, and this is as painful as you might imagine. We sometimes need to edit every single json file (or at least a subset) to add a specific key/value pair.

This would normally be relatively straightforward, using jq/bash or python, go, node, etc

mv alice.json alice.json.old ; jq '. += {"country":"uk"}' < alice.json.old > alice.json

However, our json files have comments in them

We could:

In all of these cases, we lose the comments when performing automated edits to the files


Remove all of the comments upfront

Either with sed etc. or manually

Remove all of the comments every time

With sed or Python regex replacements

Use node

Treat the files as json5