Skip to content

Instantly share code, notes, and snippets.

@cellularmitosis
Last active August 20, 2021 08:33
Show Gist options
  • Save cellularmitosis/d5e14f84d3dfdfd7eed81347fc779f06 to your computer and use it in GitHub Desktop.
Save cellularmitosis/d5e14f84d3dfdfd7eed81347fc779f06 to your computer and use it in GitHub Desktop.
JSON with comments

Blog 2020/4/13

<- previous | index | next ->

JSON with comments

Recently I saw a thread on twitter about various techniques for hacking comments into JSON:

EVfljVlX0AIWcdY

While this is clever, it... makes me feel icky.

Why was this done? Presumably because creating your own file format ("JSON with comments"), along with a JSON-with-comments to JSON "compiler" which strips the comments out, was seen as being too difficult.

But is it?

jsonc-to-json.sh:

#!/bin/bash
# Produces a .json file by stripping the comments from a .jsonc file.
infile=$1
outfile=`basename $infile .jsonc`.json
cat $infile | sed '/[[:space:]]*\/\//d' > $outfile

Nope. 😎




Might as well throw in an example Makefile 😃:

foo.json: foo.jsonc
	jsonc-to-json.sh foo.jsonc
@patrickmichalina
Copy link

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment