Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@usrrname usrrname commented Feb 2, 2018

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

@VishwaJay

This comment has been minimized.

Copy link

@VishwaJay VishwaJay commented Apr 22, 2018

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

This comment has been minimized.

Copy link

@twome twome commented Dec 15, 2018

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

This comment has been minimized.

Copy link

@nikesh9220 nikesh9220 commented Jan 24, 2019

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

This comment has been minimized.

Copy link

@lsloan lsloan commented Jun 5, 2019

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

@volandku

This comment has been minimized.

Copy link

@volandku volandku commented Nov 20, 2019

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

@MoOx

This comment has been minimized.

Copy link
Owner Author

@MoOx MoOx commented Nov 20, 2019

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

@LarsHLunde

This comment has been minimized.

Copy link

@LarsHLunde LarsHLunde commented Jan 23, 2020

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

This comment has been minimized.

Copy link

@lurkie lurkie commented Feb 10, 2020

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

@mbaetaoliveira

This comment has been minimized.

Copy link

@mbaetaoliveira mbaetaoliveira commented Mar 17, 2020

You can use:

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

@AMorgaut

This comment has been minimized.

Copy link

@AMorgaut AMorgaut commented Apr 24, 2020

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":""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.