36
Skayo
6y

I always wanted to have comments in JSON files, but I just discovered a really good alternative:
{
"//": "This is a comment",
"somekey": "somevalue"
}

Looks kinda ugly but it works! No need for special unconventional parsers and shit.

Comments
  • 35
    Please, just don't...
  • 0
  • 18
    Now put in two of these (with the same key, mind you) and see parsers break.
  • 1
    <@filthyranter>
    Depends on the parser you use doesn't it?

    //edit
    The specification doesn't mention anything about duplicate keys, so yes, it depends on the parser
  • 2
    @Skayo Yep. Some parsers just overwrite the value, others panic because they already have the value.
  • 2
    (and some others even ignore the value, which is not that bad since it is supposed to be a comment anyway)
  • 6
    If you need comments, I’ve generally created a new key for comments which you can simply ignore and the parser won’t break.

    {“_comment_for_key”: “something”, “key”: 123,”key2”:”something here”}

    Sadly having actual comments in json would be so much nicer.
  • 2
    (Many non-JS-parsers seem to use dictionaries. So they would just overwrite the value.)
  • 3
    What about JSON5?
  • 4
    @gitreflog that looks like, let’s take something simple and make it work in any use case - and have standard parsers break like all hell. 😦
  • 0
    @gitreflog Damn, that exists. Nice
  • 7
    If you need comments, JSON is the wrong tool for the job. Can you use something like yaml instead?
  • 5
    @C0D4 watch, next feature request is if/else in JSON...

    "If key X has value y I want to add key z"...

    Why make JSON complex when it's only ment for simple data communication...
  • 4
    @incognito oh don’t give them ideas 😂
    It’s bad enough I have to work with SOAP and JSON APis, I dont think I could handle conditional json too
  • 2
    @incognito Because people are using it for something it wasn't meant for. On the bright side, JSON feature requests aren't a thing. The standard is fixed.

    I wish a better standard for config files would take off. I'm not really a fan of yaml and ini files look ugly. Json is almost there, it just needs to be a little less strict and allow comments. People try to make a nice standard all the time but it never takes off
  • 4
    @filthyranter fixed it no problem

    {

    "1": {"//": "This is not a comment"},

    "2": {"hello": "bar"},

    "3": {"//": "world"}

    }
  • 3
    <@inaba>
    Oh no
  • 4
    @inaba i want to make this as offensive, but it’s so bad it’s good 😂
  • 4
    @inaba LOL this is either the best meta joke of all time or you’re actually being serious. I will go with the former since it’s really more funnier that way
  • 3
    no offense, but...

    don't use json? my personal favourite replacement is yaml, which is reasonably well supported (save for web apis where you don't have comments anyways), but there have to be alternatives.

    except xml, that's even worse
  • 4
    You are using the wrong format of you need comments
  • 1
    <@git-gud>
    Yeah but YAML always needs to be properly formatted. JSON can be in a single line.
  • 2
    @Skayo when is that a criterium, do you want to minimize it by removing whitespaces?
  • 1
    <@git-gud>
    No but I hate languages like python and yaml where the correct indentation is important
  • 1
    @Skayo well, i love them, because that removes the need for brackets, but whatever floats your boat i guess
  • 2
    If I need json with comments, I simply use yaml. It's also much easier to read.
  • 1
    HashiCorp HCL is quite nice
  • 1
    @M3m35terJ05h you might wanna take a look at TOML then https://github.com/toml-lang/toml
  • 1
    But why
Add Comment