0

today i learned that not all jsons must be enclosed in curly braces {}

a valid json can have it's outermost things be square brackets as an array []

this is a special kind of pain and despair

Comments
  • 2
    What’s your problem with that? Do you write a json parser?
  • 4
    You know what's even weirder? A simple string is also a valid json! Like this:
    "hello"

    mind_blown.tiff
  • 4
    Actually, valid JSON can also just be a null, boolean, number or string.

    Some optional whitespace normally accepted by parsers is missing, but apart from that this link should help you understand, what exactly is valid JSON: https://www.json.org/json-en.html
  • 4
    I mean yeah. Valid json is any valid json value... the outer {} is not a rule, it's a dictionary type sitting at root level. literally any valid json value can be at root level because the root node can be any valid json value. It makes sense when you think about it!

    and yeah, if you're writing your own json parser, then either you need to research the specs, or just don't and get a library that does it for you. :) No shame in using other peoples code! Though I think you could technically also use regex at every level that checks for a valid json type at :D
  • 2
    a valid json can even be simply a "string". or a number. or null. which should be no problem at all, since json parsers exist in the thousands.

    unless you're trying to reinvent the wheel by adding your own parser. in which case: _this_ is the source of your despair.
  • 0
    How is this a surprise?
    It isn’t a file format like yaml. It’s just javascript objects. An array is a javascript object. So is 13.
  • 1
    @Root Actually it is a file/serialization format like YAML. And as YAML is a superset of JSON, of course you can literally put any valid JSON in a YAML file.
  • 0
    To clarify, I've been so used to the jsons I've seen and worked with always being {} wrapped I just assumed that the outermost {} was a requirement. No parser writing or anything fancy.
Add Comment