Skip to content

Instantly share code, notes, and snippets.

@MoOx
Last active September 27, 2024 07:51
Show Gist options
  • Save MoOx/5271067 to your computer and use it in GitHub Desktop.
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"
}
@lsloan
Copy link

lsloan commented Jun 5, 2019

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

@volandku
Copy link

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

@MoOx
Copy link
Author

MoOx commented Nov 20, 2019

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

@LarsHLunde
Copy link

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
Copy link

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
Copy link

You can use:

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

@AMorgaut
Copy link

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":""
}

@erwinpratama
Copy link

I'm also looking for this.

@SchreinerK
Copy link

SchreinerK commented Aug 29, 2021

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
Copy link

hemraker commented Jan 6, 2022

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

@xgqfrms
Copy link

xgqfrms commented Apr 14, 2022

@masterewot
Copy link

I found it

@lsloan
Copy link

lsloan commented Oct 17, 2022

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

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.

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