16 ms·
JSON with Commas and Comments
- aaron-santos 6y ago> Yes, Doug Crockford deliberately removed comments from JSON but people keep putting them back in. If we’re going to have comment-enriched JSON (e.g. for human-editable configuration files), we might as well have a standard one. The text "deliberately removed comments from JSON" links to https://web.archive.org/web/20150105080225if_/https://plus.google.com/+DouglasCrockfordEsq/posts/RK8qyGVaGSr https://web.archive.org/web/20150105080225if_/https://plus.g... where Doug not only explains the reason, but also a solution which works with the existing standard. It's imperative that the JWCC author explain what they find lacking in Doug's solution - stripping comments before handing off the a JSON parser. A new format is a really weird way to go about fixing a problem that already has a solution.
- Supermancho 6y ago> a practice which would have destroyed interoperability A baseless reason is not a compelling reason. Some imagined eventual use is not relevant to the utility and convenience. Interoperability (in this greater undefined sense posited) cannot be maintained via syntax anyway. > stripping comments before handing off the a JSON parser. Putting an extra processing step before use of JSON is impractical.
- aaron-santos 6y agoThe base is the part right before that. > I removed comments from JSON because I saw people were using them to hold parsing directives > Putting an extra processing step before use of JSON is impractical. Care to explain?
- saurik 6y agoPeople want to use JSON as on disk configuration files, and that's where they want the comments and trailing commas. You can either generate the configuration file from another file, which is frankly stupid annoying and leads to repositories with two copies of the same information, or modify the software that reads the configuration file to strip it before handing it to the JSON parser... which clearly "solves the problem" but also begs the question "ok, so what is the format of the actual confirmation file, then?". The answer to that latter question is more important, as the goal would then be to get everyone to standardize around that format, at which point (I guess) "JSON" would be obsolete as no one would use it and everyone would be using the new JSON wrapper format and all implementations would be designed to have a way to read that format... but like, at that point, you are just making a weird semantics argument. I think the core problem is we just have too many of these... like, what happened to JSON5? We need some reason to all rally around a single specific format of "JSON, but with comments and trailing commas (and I will personally request multi-line strings)".
- Supermancho 6y ago> I removed comments from JSON because I saw people were using them to hold parsing directives Java has not been destroyed, nor any other language that uses injected behavior. Ironically, applying parsing directives are basically another tool being run (similar to a linter) so I'm not sure what he's even getting at when suggesting running yet another tool. This just complicates the ecosystem and is part of what makes javascript seem so primitive in use and syntax. Features that are counterproductive for poorly considered reasons.
- oblio 6y agoSimple example. JSON is frequently used for configuration files. Comments break syntax highlighting for JSON. Similar thing if you want to inspect a JSON dump into a file.
- BugsJustFindMe 6y agoCrockford regularly displays a stunning degree of tunnel vision. Without a standard format for comments, you have no reason whatsoever to expect the ad-hoc comments in a JSON file made by anyone who isn't you to be styled like JavaScript comments. And if your response is "then everyone needs to use JavaScript-style comments", well, TA-DA, you've just added comments to the spec. You can't have it both ways. > It's imperative that the JWCC author explain what they find lacking in Doug's solution - stripping comments before handing off the a JSON parser If the syntax rules for adding comments are unspecified, then the the syntax rules for what to strip out are unspecified too!
- ethanwillis 6y agoIf the rules are unspecified the only thing you wouldn't be able to put in comments are what is specified as JSON syntax. If you add minimal specifications for escape sequences then even that is allowed.
- BugsJustFindMe 6y ago> If you add specifications then Indeed.
- tshaddox 6y agoI wish JSON supported comments, but I don’t really understand your criticism. You say: > Without a standard format for comments, you have no reason whatsoever to expect the ad-hoc comments in a JSON file made by anyone who isn't you to be styled like JavaScript comments. But, since comments are not supported in JSON, you can expect JSON from external sources to contain no comments whatsoever. That’s precisely Crockford’s point: he didn’t want the format to have a comment syntax that works across different parties so that the only way to use comments is internally, i.e. with a custom comment syntax that you strip before interacting with external sources.
- hn_throwaway_99 6y agoI can't believe that anyone who has ever wanted to comment a package.json file could think Crockford's argument is anything but nonsense. The problem is these files are both edited by humans, and by a plethora of tools. Without a standard comment format, it's been a nightmare trying to comment package.json file in a way that doesn't break something in the NPM/Node ecosystem.
- colejohnson66 6y agoIf I’m going to use “JSON with comments” that I have to run through a preprocessor before parsing, why not just use a language where comments are explicitly allowed (YAML, TOML, etc.)? Crockford’s answer is not a solution. His words can be rewritten as: “you want comments? Use a different language that looks and feels like JSON, but isn’t.” All that does is add confusion. The only reason JSON is as popular as it is is because JavaScript parses it natively (JSON.parse is defined by the JS spec).
- redisman 6y ago> The only reason JSON is as popular I mean I remember using XML for serialization back in the day and how it made we want to kill anyone who was involved in its creation. JSON won because it’s a great and simple standard and yes because it maps 1:1 to js. It was very popular with people who hated js when it came out too - just because of how much better it was than the other options
- hn_throwaway_99 6y agoI think the move from XML to JSON mirrored a lot of the overall switch in the software industry at large from unnecessarily overly complicated "design-by-committee" specs to simple, get-shit-done practicality. (Heck, anyone remember the horror that was the original EJB spec, or god forbid, CORBA?) The rise in scripting languages like Python and JS on the backend is another example of this.
- enw 6y agoAnd I see this obsession in some people in the industry trying to prematurely optimize every drop in their code, but they don't understand the added complexity results in inefficiency (mostly lost improvement potential) in the long-term.
- ledauphin 6y agothe very simple thing lacking in this solution is that sometimes you want to programmatically _add_ comments to (or preserve comments within) some output file format. If they must be stripped before parsing, then it is definitionally impossible to do either of these things. It's not unlike saying "actual code shouldn't allow comments - you can have a separate preprocessor in your non-code file that strips those out." Sure, you could do that, but all your line numbers would be meaningless and everyone would hate the language from day 1.
- Lazare 6y agoGiven the number of times people have invented and re-invented JSON + comments, I think it's clear there is a demand for JSON + comments, and that Crockford's view that you can solve everything by asking people to manually strip the comments out before parsing the JSON isn't viable.
- jtdev 6y agoPetabytes of JSON formatted data flowing across the internet every second of every day disagree with your assessment.
- Lazare 6y agoAnd I suppose the vast amounts of XML (still!) flowing across the internet show there's no need for JSON at all, so the real mistake was even inventing JSON? (Also, as gets pointed out every time this topic pops up on HN, including multiple times on the submission already, the main demand isn't people wanting to add comments to the JS flowing across the wire, but to config files sitting on disk. Responding to people saying "feature X would help use case Y" by noting how many people are using it for use case Z even without feature X is logically incoherent.)
- jtdev 6y agoCrockford (and many others) found that the many flaws inherent to XML warranted a new specification, so JSON was born. If you have an issue with JSON, go create your own spec, don’t shit all over the perfectly capable, simple, elegant specification that is JSON.
- Lazare 6y ago> If you have an issue with JSON, go create your own spec And many people have. You're literally posting this in response to someone submitting the new spec they created to solve this. I don't understand what you're trying to argue here.
- coldtea 6y ago>Doug not only explains the reason Just because he had a reason to give doesn't mean it wasn't a bad reason. His stated reasoning failed with hindsight. (a) People already do what he thought was preventing (custom parser behavior) by removing comments, (so his choice failed to prevent what he wanted to prevent) (b) people have already created dozens of JSON parser variants that accept comments, because we really want those. And not just some niche devs - Microsoft and dozens of other big companies have tools that accept JSON + dangling commas + extra comments (so his choice was second-guessed and bypassed anyway, just in ad-hoc ways instead of a better universal one). It wasn't his place to define whether we get comments or not based on some potential parser abuse. But he did it, and now we're stuck with that decision...
- sagonar 6y agoI disagree, he has DONE a common data format that works. If you do mod it, for example add comment to the data sent, 99.9% of the people agree, it is no longer JSON.
- coldtea 6y agoWhich is neither here, nor there. People still do it, and they still call it JSON. Heck, many JSON parsers, who otherwise work fine with regular JSON still accept it. Even parsers using a different name like JSONC still allude to the JSON connection - and still are an argument that people found a lack in JSON. Etymology/originology never produced much benefit over pragmatic non-prescriptive examination of how people use things. If anything, it helped confused the situation with pedantic objections.
- rswail 6y ago> It wasn't his place to define whether we get comments or not based on some potential parser abuse. But he did it, and now we're stuck with that decision... He was writing the specification, of course it was his place to do so. Who else would be in the right "place" to write his own specification that no one was enforced to use.
- exporectomy 6y agoOh, it's because people were abusing them. I thought it must have been that you can't read a file then write it back out without either losing the comments or having a whole special data structure to store them in.
- willtim 6y ago> A new format is a really weird way to go about fixing a problem that already has a solution. Stripping comments before handing off to a JSON parser is not a full solution. For example, if the file has to be augmented with additional data and written out again, the comments would be lost.
- wruza 6y agoFormatting would be lost too (multiple newlines, array formatting, wrap limits, etc), and the order of keys. There are two separate tasks: comment json for a human and ignore that on parse(), and maintain pretty-json with all its usefulness programmatically with parseWithSyntaxStructure(). We shouldn’t mix them into one single problem, because in one case you just want multiple readers (a human and a computer) and in the other you want to rich-edit a human representation on its own.
- virgo_eye 6y agoIf the solution is to store config in a format with comments and strip them off before handing to JSON, what should that format be? Shouldn't it be formalized? Couldn't JWCC be that format?
- EdSchouten 6y agoHere's another flavour of JSON with comments: https://pypi.org/project/commentjson/ https://pypi.org/project/commentjson/
- xpe 6y agoAlso, there is JSON5, which I think is more widely known https://json5.org https://json5.org https://github.com/json5/json5 https://github.com/json5/json5
- zeroimpl 6y agoRather than keep making new variants of JSON, it'd be nice if somebody could convince some mainstream language maintainers to just update their built-in JSON parser to add optional features like skipping over comments and not caring about trailing commas. Most parsers support various flags already to configure things, so there could just be an ALLOW_COMMENTS flag and ALLOW_TRAILING_COMMAS flag. As a case in point, the python parser breaks the JSON spec already with regards to Infinity/NaN, but then has flags to configure this. See https://docs.python.org/3/library/json.html#module-json https://docs.python.org/3/library/json.html#module-json > The RFC does not permit the representation of infinite or NaN number values. Despite that, by default, this module accepts and outputs Infinity, -Infinity, and NaN as if they were valid JSON number literal values:
- xpe 6y agoI can see why this example appeals to you. However, asking implementors to go outside of a spec is _in effect_ making a new JSON variant. Once you see this, you can understand why people want a spec with a name.
- spankalee 6y agoIt's going to have to be a different mode and mimetype because this format would break all kinds of parsers. I wish something would done in this area relatively soon though because JS has added a number of features that would make a new JSON much nicer like multi-line strings and BigInts. I think JSON + comments, commas, template literals, BigInt, NaN, Infinity, and BigDecimals (if/when those land in JS) would be very useful. (It'd be nice to include dates, but that's tricky w/o a literal and because dates)
- fiddlerwoaroof 6y agoJSON doesn’t specify its numeric types: the mapping of a string of digits to concrete numeric types is implementation-defined: so, JSON doesn’t need specific syntax for BigInts or arbitrary-precision decimals.
- 6y ago
- uDontKnowMe 6y agoIMO it would also be really nice to have non-string keys in objects, like: { [1, 2]: "a", [3, 4]: "b" }
- resonantjacket5 6y agoit's usually a pretty bad idea to have a mutable object (the array) as a key. Though I guess if you consider it as a 'tuple' since when in json form it can't be modified I guess it'd be technically possible, though with some heavy caveats.
- chrisseaton 6y agoThere’s no concept of mutability in JSON. A JSON parser could give you immutable arrays and maps if it wanted to.
- resonantjacket5 6y agoYes, but json by itself doesn't actually do anything. It'd always be used in context of javascript/python/ etc.. As I said you could interpret it as a tuple or I guess a frozen array for javascript? I don't think there is a native immmutable array in javascript. If it's a map I'm a bit more unsure how you'd check to find the object (quickly). You're basically getting into the how to store a struct/class as a map key. Either way its a bit more involved than just having json parser return an immutable array and map, and the more I think about it the more edges cases there are.
- recursive 6y ago> I don't think there is a native immmutable array in javascript. I'm here to tell you that `Object.freeze()` works just fine on arrays.
- resonantjacket5 6y agoNice. Though I don't think that solves the rest of the issues I outlined sadly. I guess you could have this json+ convert into some custom javascript class that allows arrays/objects as map keys rather than the normal javascript object that only accepts strings as map keys.
- xpe 6y agoSee also: https://json5.org https://json5.org https://github.com/json5/json5 https://github.com/json5/json5
- xpe 6y agoCombining effort with JSON5 (~4K GitHub stars) makes sense to me.
- CameronNemo 6y agoThey explicitly do not want unquoted keys. The author reviewed most prior art in TFA.
- xpe 6y agoWould you like to spell out what TFA means?
- pseudalopex 6y agoThe fine article.
- dharmab 6y agohttps://en.wiktionary.org/wiki/RTFA https://en.wiktionary.org/wiki/RTFA
- xpe 6y agoI'm aware, thanks. I wanted to see if CameronNemo would spell it out. I'd like to point out the HN Guidelines: https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html > Please don't comment on whether someone read an article. "Did you even read the article? It mentions that" can be shortened to "The article mentions that." Even putting aside what "F" stands for, this means that comments like TFA or RTFA are not in the spirit of the discussion here.
- wwww4all 6y agoThere’s already JSON with comments, called XML. It’s Extensible! Been there, done that. JSON became popular data format because it didn’t have all the overhead of comments and extensibility.
- xpe 6y agoThis comment is off the mark. You are conflating comments with extensibility. They are very different.
- crazygringo 6y agoWhat's nice is that this can essentially be ignored by API's and browsers. Computer-generated data never needs to include trailing commas or comments. Really the principal use case here is JSON as human-edited configuration files. In which case I suppose it's nice to have a name like "JWCC", but really it's just two flags for JSON decoding libraries to add (or a single nice combined flag). So that really would be great IMHO, if you could just call json_decode($json, JSON_ALLOW_JWCC). I don't think we need a new MIME type or anything. But a file extension of ".jwcc" would be a nice convention too.
- laurent92 6y agoIt doesn’t improve the dev experience if the compilers don’t produce it, and this is really about dev comfort. You need to be able to modify JSON programatically and keep the comments, if the output is pretty-printed. Also, NaN numbers are an entire features, which they talk about above.
- wruza 6y agoBut why should re-jsoning keep //-comments? I never met the need for that personally. Comments help initial filename.json to be parsed by human eyes, and when it is sucked into a program (and possibly transformed), they lose meaning, even when pretty printed. For them to stay, one can put comments into real keys and/or structure their data appropriately. Even if one ultimately needs to keep json document’s human structure (like key order, comments, whitespace nuances), they may create and use more syntax structure-aware library to do that.
- laurent92 6y ago> they may use more structure-aware library to do that Sure. I’m particularly thinking about mvn upgrades or “npm update”, which modify pom.xml/package.json files to upgrade the libraries, after checking rules (non-breaking changes or not, vulnerabilities or latest, etc). For mvn, libraries never succeeded to modify the pom in-place without wrecking the file format, so mvn upgrades never became a thing. It’s also a demonstration that a DOM with comments in memory doesn’t ensure we can output the file as-is ;)
- ludocode 6y agoI wholeheartedly agree with this idea. In case anyone wants a fast C++ parser for this, RapidJSON [1] has optional support for comments and trailing commas; I actually added trailing comma support to it. I did this to support hand-writing game data in JSON (e.g. monster stats, campaign dialog scripts) and then converting it to MessagePack at build time [2]. This gives you very high simplicity (no schemas) and excellent performance. [1] http://rapidjson.org/ http://rapidjson.org/ [2] https://github.com/ludocode/msgpack-tools https://github.com/ludocode/msgpack-tools
- treve 6y agoThe page addresses that there are other alternatives/supersets. I think the most common one that has decent adoption is JSON5. I would like to see why the author chose to do yet another format, vs adopt JSON5.
- AtlasBarfed 6y agoAren't these deserialization options already in, say, Jackson?
- ludocode 6y agoThese options are already in lots of parsers. That's part of why this proposal is attractive: we don't need much to start using it. In fact I've been using JSON with comments and trailing commas in my own projects a lot already, and I imagine lots of other people have converged to the same idea since these are really the only two pain points when handwriting JSON. It's about time we give it a formal name.
- jinkyu 6y agoI recently did a thing in YAML because I needed to deserialize the files at runtime and one of the fields is a multi-line Kusto query. if I ever need to hand generate a files with multi-line text in it again, I won't use json.
- jugg1es 6y agoWhy can't these no-brainer features just be added to browsers without a new standard!!
- yedpodtrzitko 6y agoYou mean like when Google implements some Chrome-only features crippling other browsers, so people are indirectly forced to switch to their browser? We've been through the era of "this website works only in IE6 and better" already and I cant remember a single advantage it would bring.
- onion2k 6y agoThat would make fetching JSON data significantly more complicated. You'd need to write your fetch call to detect if the browser supports comments and commas in JSON, and then make the request to the server to ask for whichever variant the user's browser supports, presumably with a different Accept header. Users who haven't updated to a new version of the browser would still ask for the old JSON format, and users who have updated would ask for the newer version. That would mean your API would need to serve different variants based on the Accept header, and your backend code would need to generate the two different variants. And then you'd have to question if sending API responses with unused things like comments is actually a good idea given it's entirely wasted data in a production environment. I suspect few people would use a newer JSON format until it's been available for a long time, and even then they wouldn't use it on big sites.
- jugg1es 6y agoyea but it would be cool if you could include comments, wouldn't it?
- jtdev 6y agoI actually think the lack of comment support in JSON has resulted in better naming and better documentation by those who create JSON to be consumed by others. I don’t want comment support in JSON at this point personally.
- dvh 6y agoI agree. Also if json had comments, people would start putting data in comments so soon you would have to parse comments as well. Then someone would ask for ' for strings and whole json would turn into garbage.
- skeeter2020 6y agoCould we solve dates first, then worry about comments and commas?
- Swizec 6y agoISO-8601 not good enough for you? It’s a standard, human readable, has every datetime detail, every language that supports JSON can read it. What’s missing?
- tshaddox 6y agoBe careful ever saying “every datetime detail”! An obvious one is that ISO 8601 can’t represent the location of the local time with enough information to know the current (or historical) daylight saving time rules for that location. You might not need that now, but you might if you’re building a calendar app! ISO 8601 can represent a simple offset from UTC, but it can’t do a lot of the things that the IANA time zone database can do.
- Swizec 6y agoFair, very fair. Have gotten bit by that in the past. Adding an IANA library to the ISO8601 library (often part of the same lib) has so far solved that problem for me. I’m sure it starts struggling with dates before the early 1800’s though. Also doesn’t work well if you’re dealing with relativistic effects I think.
- ncallaway 6y agoI've never had a problem with dates by always serializing them as ISO-8601. It absolutely has every benefit you cite. It's the only ISO spec I know the name of without having to look it up.
- fiddlerwoaroof 6y agoIs the -0700 offset PDT or MST? ISO-8601 doesn’t solve a whole bunch of problems related to locale-aware date math.
- qbasic_forever 6y agoAlternatively, just use JSON as a low-level data interchange much like a CSV file. Put all your human-managed content in a configuration language that emits JSON, like jsonnet, cue, dahl, etc. These languages add comments, function, variables, and much, much more (like fancy data cascades, validation, schemas, imports, etc.) to make managing configuration at scale easy.
- Ballas 6y agoWhy even go to the trouble of converting it to json then?
- zapita 6y agoFor interop with the massive json ecosystem.
- qbasic_forever 6y agoIt simplifies your code that depends on configuration. Instead of having to parse the config language you just read the JSON. Pretty much every programming langauge has a JSON parser built in to its standard library, while support for higher level config langauges is a lot less ubiquitous. It also makes it easier for other tools to generate, validate, or output config that feeds into your system. They can do whatever processing they want and emit plain old JSON.
- glennpratt 6y agoJust use YAML. JSON is valid YAML.
- _ZeD_ 6y agoProblem is... YAML sucks, as human readable format
- crubier 6y agoNo it doesn’t. I know the HN-crowd feeling about this, but no, YML doesn’t suck as a human configuration language. TOML or XML properly suck. YML is actually a superset of JSON with comments and less brackets if you want. It’s widespread and supported and fulfill the need of OP, so no need to add yet another format. Or use JSON5. But please not yet another standard
- eknkc 6y agoHow is TOML bad as a config language? I would not transmit data in TOML but is makes a lot of sense as a config language.
- 0xbadcafebee 6y agoNeither YAML or TOML are config languages, they are data formats. A data format is a way of structuring or serializing data. The whole point of the format is to read and write arbitrary data. It may have a simple schema, or features designed for the loading and unloading of data. But all those features are designed to assist the machine, not the human operator. A config format is designed specifically to assist a human operator, not a program. Humans should not have to consider data types when they write a config, or when they feed it to a program. Humans should have useful features to make their lives easier, like variable substitution, pattern matching, inheritance, namespaces, simple newline-separated whitespace-indifferent commands, etc. Programming languages are just elaborate configuration formats. And that's where the problem begins: how much "power" do you give the configuration format before it gets unruly? It's difficult to find a balance. People end up using simple data formats because the parsers are widely available and they can "fake" advanced features by making programs interpret a specifically-crafted data structure as a configuration instruction. But there's very little thought put into how this can be extended to make more complex configurations easier. Most web servers and other complex software [usually run by sysadmins] have a real configuration format or configuration language. Poorly-written software pretends a data format is a programming language, or even worse, forces you to use an actual programming language. These designers fundamentally don't understand or don't care about the user.
- crubier 6y agoWell at least the article is aware of its own vanity. I was going to cite the XKCD about “standards” but it did for me. Just use JSON5 and stop losing time (or YML, yes it’s not that terrible for configuration files)
- nanoscopic 6y agoBeen there, done that: https://github.com/nanoscopic/ujsonin https://github.com/nanoscopic/ujsonin "JSON" parser in both C, Perl, and Golang. All three support "JSON" with or without commas as you desire and comments.
- zapita 6y agoI’ve been using Cue (https://cuelang.org https://cuelang.org) as a human-friendly superset of json, and it’s a breath of fresh air. The creator is one of the original authors of GCL, Google’s core infrastructure configuration language, and you can tell he poured all the hard-earned lessons from that into Cue. Highly recommended.
- deleted 6y ago[deleted]
- SPBS 6y agoI've been finding plain JavaScript as a very nice alternative to the many config file formats out there. It already has the trailing commas + comments bells and whistles, plus the ability to do computation to generate repetitive elements. Of course there's always the danger of the user creating a monstrosity of a 'config file' but if they're the ones using the software that consumes the config file, that's on them to keep the config file complexity down. Go already has a very useful JavaScript engine written in pure Go (goja), I recently opted for that as the config file format over templated JSON/TOML/HJSON or Starlark/Tengo because JavaScript is well defined and expressive. I'm thinking it will be a good choice for an authorization rules engine as well (over custom DSLs like OPA/Casbin) because using it is so much simpler if the user already knows JavaScript.
- stareatgoats 6y agoThis is my preferred solution as well, nowadays.
- jobigoud 6y agoTuring complete config files makes me a bit nervous.
- q3k 6y agoNot just turing complete, but impure. Configuration files shouldn't be able to perform arbitrary IO. Starlark to solve this exact problem, and is a Python subset so that people don't have to learn something totally new either. It's still turing complete, but at least guaranteed to not have any side effects, or even be able to access anything but an explicitly given execution context.
- telekid 6y ago@borkdude’s sci actually seems like a nice contender for this as well.
- geraldbauer 6y agoFYI: I collect JSON variants with extension at the Awesome JSON - What's Next? page [1]. [1] https://github.com/json-next/awesome-json-next https://github.com/json-next/awesome-json-next
- oneeyedpigeon 6y agoI wonder if there's an equivalent for markdown variants. I wonder if there are more markdown variants or JSON variants.
- nly 6y agoLet's stop pretending JSON isn't popular because it lets us be loosey-goosey with our specifications. Writing specifications is hard and time consuming, and getting people to follow them is almost impossible. JSON is something we can (almost) all agree on for dumping loosely specified, human readable representations of data structures. It lets users and client application developers be lazy and not have to learn a new library for our preferred data format. The lack of schemas or validation means we can often get away with wishy-washy, hand-written, plain English specifications, or specifications "by example". It's convenient. For anything intended to be robust, for serious data interchange, use something else. Anything with support for machine-readable schemas, validation, and robust encoding rules. Binary is preferable but a JSON serialization is still a must-have escape hatch. And here's a shocking idea... provide your clients with an SDK. It's pretty easy to build one around a multi-language data serialization framework and maintaining an SDK is still easier than maintaining a spec that your clients will probably implement incorrectly.
- jonathanaird 6y agoI really want something that’s strongly typed but doesn’t require code generation like protobufs do. Yaml doesn’t do it for me. The closest I can get is putting the type guarantees in the database and using GraphQL.
- speedgoose 6y agoI use JSON Schema to validate JSON documents. https://json-schema.org/ https://json-schema.org/
- nly 6y agoImho, statically typed languages are the ones that benefit most from schema. The current schema version is 12 but the implementations for Go, Rust, C++ and Java are all listed as draft 7. None of them support codegen either, just validation, so not exactly compelling.
- 6y ago
- fpoling 6y agoFor comments in plain JSON a common approach is to use keys like "comments" or "description" in object literals. As for trailing comma the only real issue in practice is noisy unified diff output. But for JSON word diff or similar works better in any case when the trailing comma is not an issue.
- mschuetz 6y agoAdding a property to an object that does not belong to the object is pretty much the worst workaround to comments, in addition to it being much harder to read due to lack of comment specific syntax highlighting.
- majkinetor 6y agoThat is not only issue in practice. I can't count the number of times when somebody changed the json adding single line thinking what could go wrong without test...
- argvargc 6y agoPeople use JSON For configuration files? What a PITA. Use a tool suited to the job, eg, TOML. https://en.m.wikipedia.org/wiki/TOML https://en.m.wikipedia.org/wiki/TOML Not that there's anything wrong with adding comments and commas to JSON. Call it JSONv2, keep the .json extension and let people bump around upgrading for a bit. It's hardly much of a change but has significant benefits, even outside the config file use. For instance it can sometimes be useful to annotate raw data, and it'd be nice to have that built in. Certainly the commas is a no-brainer.
- sandreas 6y agoLooks pretty similar to rome json (https://rome.tools/#rome-json https://rome.tools/#rome-json)
- hermitcrab 6y agoI just happen to be working on JSON parsing today. What a mess. People don't even follow what is already there. I've seen several 'JSON' data files like this: {<object>} {<object>} That isn't valid JSON, is it? The Qt JSON parser doesn't handle it.
- dak1 6y agoThat looks a bit like NDJSON, assuming that the space between the objects is actually a newline. There are libraries[1] that support parsing it and it's not too hard to do yourself, either. Some fairly popular projects use it to represent multiple responses in a single response body[2]. [1] http://ndjson.org/libraries.html http://ndjson.org/libraries.html [2] ElasticSearch uses it for msearch response bodys, for example.
- hermitcrab 6y agoIt looks like it might be this: https://jsonlines.org/ https://jsonlines.org/ Which I hadn't heard of before.
- ydant 6y agoOr JSON lines - whose website looks very similar to NDJSON's: https://jsonlines.org/ https://jsonlines.org/ This format works great when using Amazon Athena (Presto) against log files written with one JSON object per line.
- quietbritishjim 6y agoBoth the websites are fancy ways of saying "one JSON object per line". The idea of laying out data that way was in common use before either of the website, they're just recording the practice.
- 0xbadcafebee 6y agoJSON is a data format. You actually want strict formalism in a data format, because the integrity of the data matters more than flexibility. Comments would only serve to make humans edit the file by hand more than they do now, and to maintain it like a snowflake. You should not be maintaining your data in JSON, it should only be a momentary stop along the way to a more robust data storage system [with a schema]. Optional end commas would only encourage people to use shitty hacks to try to craft JSON without a real parser or encoder, which would 1000% end up with broken JSON all the time, which would then force vendors to support shitty broken JSON. There's a reason it's designed like it is. Cutting corners is not going to result in better outcomes.
- blacktriangle 6y agoI'm honestly surprised EDN hasn't taken off as a JSON alternative. It keeps all the good things about JSON but then adds the features people want like comments and more clear specification. The ability to tag elements to extend for specific types while allowing intermediaries to not care about those types is incredibly powerful.
- peterohler 6y agoFor even more flexibility in JSON format there is SEN. https://news.ycombinator.com/item?id=26237048 https://news.ycombinator.com/item?id=26237048 talks about making JSON look good and introduces SEN at the end with optional commas and quotes. It also allows comments and converts back and forth with pure JSON with no loss of data. Comments are obvious lost though as JSON does not support comments.