Skip to content

Instantly share code, notes, and snippets.

@MoOx
Last active May 11, 2026 06:46
Show Gist options
  • Select an option

  • Save MoOx/5271067 to your computer and use it in GitHub Desktop.

Select an option

Save MoOx/5271067 to your computer and use it in GitHub Desktop.
How to make comment in JSON file
{
"//field": "These 'double quote' 'double quote' are used as comments, because JSON doesnt' allow comment",
"field": {},
"#another-field": "Another comment",
"another-field": {},
"/*stuff": "Be careful to use them when you have full control of the content :)",
"stuff": [],
"bla": "bla"
}
@usrrname

usrrname commented Feb 2, 2018

Copy link
Copy Markdown

Aww damn! I landed on this page from a google search looking for an easy workaround

@VishwaJay

VishwaJay commented Apr 22, 2018

Copy link
Copy Markdown

Another way, if you have a context checker that doesn't allow empty quote strings for declarations:

{
    "_comment":  "Comments with // or /**/ are NEVER used in a JSON file!!!",
    "regulardata": "stuff"
}

Hope that helps. Just have your processing code lack processing for it, and it works fine.

@twome

twome commented Dec 15, 2018

Copy link
Copy Markdown

JSON (& strictly-linted JS) don't allow duplicate keys of objects, so add a unique letter or number to make it validate. This is a trick I've seen somewhere in popular code but can't remember where, sorry. I think the convention of using a "//" as the basis for comment names seems fairly sensible & terse, and should make it easy to programmatically update current JSON configs to some better format in future.

{
    "//a": "This is the first comment."
    "//b": "...and this is the second." 
}

@nikesh9220

Copy link
Copy Markdown

The JSON should all be data, and if you include a comment, then it will be data too.

You could have a designated data element called "_comment" (or something) that would be ignored by apps that use the JSON data.

You would probably be better having the comment in the processes that generates/receives the JSON, as they are supposed to know what the JSON data will be in advance, or at least the structure of it.

{
"_comment": "comment text goes here...",
"Key":"value"
}

@lsloan

lsloan commented Jun 5, 2019

Copy link
Copy Markdown

@twome's comment here is the best suggestion. I'd give it a 👍 if I could!

@volandku

Copy link
Copy Markdown

nothing works in php (json_decode) - all comments are still in data

@MoOx

MoOx commented Nov 20, 2019

Copy link
Copy Markdown
Author

Yeah because it's a trick, not real comment. JSON specs just don't have comments...

@LarsHLunde

LarsHLunde commented Jan 23, 2020

Copy link
Copy Markdown

None of these is how I would have done it,
I would have gone for something like this instead:

{
    "Comments": [
        "Lorem ipsum dolor sit amet,",
        "consectetur adipiscing elit.",
        "Suspendisse vitae risus ligula.",
        "Fusce vel urna turpis."
    ]
}

Then you won't have to think about making identifiers for every line, and it validates.

@lurkie

lurkie commented Feb 10, 2020

Copy link
Copy Markdown

JSON 5 (https://json5.org/) does support comments. However, support for it is limited. E.g. PHP does not support it yet.

ghost commented Mar 17, 2020

Copy link
Copy Markdown

You can use:

/*
{
"Comments": [
"Lorem ipsum dolor sit amet,",
"consectetur adipiscing elit.",
"Suspendisse vitae risus ligula.",
"Fusce vel urna turpis."
]
}
*/

@AMorgaut

AMorgaut commented Apr 24, 2020

Copy link
Copy Markdown

other options
put all the comment in the key :-)
there should be less duplicates problems
ex

{ 
  "// this is fun":"",
  "// this is also fun": "",
  "whatever": {
    "// other scope": "",
    "// this is fun": ""
  },
  "port": 8090, "// can't use native port":""
}

@erwinpratama

Copy link
Copy Markdown

I'm also looking for this.

@SchreinerK

SchreinerK commented Aug 29, 2021

Copy link
Copy Markdown

Because JSON is a subset of YAML, # should be used as comment character. IMHO :-) but as with everything one could argue about it
The YAML port: 8090 #can't use native port
could be converted to
a) "port": 8090, "#can't use native port":""
b) "port": 8090, "#":"can't use native port"

@hemraker

hemraker commented Jan 6, 2022

Copy link
Copy Markdown

Change the file extension to .json5 and you can haz comments 🙂

@xgqfrms

xgqfrms commented Apr 14, 2022

Copy link
Copy Markdown

@masterewot

Copy link
Copy Markdown

I found it

@lsloan

lsloan commented Oct 17, 2022

Copy link
Copy Markdown

Hjson, at https://hjson.github.io/, works better for me than JSON5. YMMV.

Its Python module has been maintained more recently and its documentation shows the syntax of multiline comments as well as #-based comments.

@Alice12s

Copy link
Copy Markdown

You can create a special key in your JSON Mapquest Directions object to store comments. This key would be ignored by any JSON parsers that don't use it, but it allows you to include notes.

@drivingdirections01

Copy link
Copy Markdown

This is a trick I've seen somewhere in popular code Mapquest directions but can't remember where, sorry.

@safepestsol

Copy link
Copy Markdown

You can add a dedicated key to your JSON Safe Pest Sol object for storing comments—JSON parsers that don’t recognize it will simply ignore it, allowing you to include notes without affecting functionality.

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