JSON as configuration files: please don’t (2016)
arp242.net
arp242.net
Also, check out JSON schema and its integration into tools like VSCode. VSCode gives you generalized autocomplete on its own config and known config formats like package.json by pulling in json schema definitions. It works well. Easily a better experience than the executable-program-as-config I learned to hate in the JVM world, and easily better than ad-hoc config like Caddy's.
I have a more practical pitch: make your tool consume json5 as well as json.
Also, it's telling that the author wasn't even willing to stick their neck out enough to pitch a single solution. I guess in the end they realized there are always trade-offs and any alternative would have its own downsides. :) But I'm getting a bit fatigued of these you're-doing-it-wrong posts. They are effortless to write and they're like crack to HN. I can complain about literally everything.
- xml contain comments, sooner or later someone will try to add data to comments, but you already have the thing for data, the format itself, adding data to comments is stupid
- unlike js, in json someone finally said "Enough! There will be only one type of quotes", it makes parser simpler
That's easily the worst argument against comments I've ever heard! Would you argue against comments in code because some jackass could abuse it to insert custom compiler directives or something? Ridiculous. The good comments do easily outweighs any harm some moron is going to do to himself by parsing comments for data.
I don't like XML any more than the rest of you, but praising json for not having comments just smacks of sour grapes.
And the point of the article is that JSON is fine for data, but configs are not data. Configs are code.
It's definitely the case that XML has features that aren't needed -- or at least, we've survived without them thus far -- but there definitely feels like more than just a smidge of only realising later why some of that complexity was present. See also: npm vs pretty much any other dependency management system.
inability to properly use a library should not count against it
JSON is a fine format for storing configuration data, and any reasonably structured data. So is XML, protocol buffets, what have you.
JSON is a poor format for humans. For a configuration file that an actual human would edit by hand, and read and try to understand later, JSON is a pain.
Comments are mostly useless in a machine-generated and machine-consumed file. They are indispensable for humans, because they are a link to what machines are not doing.
Nice formatting is useless for a machine-consumed file, because they process a stream of bytes. They are indispensable for humans who recognize text very differently.
This boils down to one thing: config files are source code, and need all the amenities that make source code comfortable for humans.
Depends on which kind of humans.
For the regular non tech types I agree JSON is poor. A couple of years ago I created a mini spec for a config file so that very low tech paper designers could configure an interactive book.
For programmers, JSON works fine in my experience.
The author mentions this, and I very strongly agree. What JSON significantly misses is the ability to have comments in your configuration files. I'm not going to hold up the Apache config files as an example of amazing config formats, but... here's an example: https://svn.apache.org/repos/infra/websites/cms/webgui/conf/...
That's awesome. It describes options that are available and not enabled, and what they do. Right. In. The. File. Comparing that to, say, webpack config... there's a pile of research needed to figure out what options are even available, let alone what they do.
All the other arguments for or against it boil down to personal preference, but not having comments is a real miss.
Programmers will maybe cope better but just being a programmer doesn't make you magically immune to poor readability and lack of comments.
Most of the points in that article apply to programmers as much as non-programmers.
(I agree there is a reasonable debate to be had about programmability - I'm on the fence about this one)
Cloud Formation is a great example of the failures of JSON. Specifying just a single dynamo table with indexes and scaling roles in JSON is a hard to read cluster of a mess. Even with a good idea and json collapse / expand help, large json files are just insufferable to deal with as a human
But do you know what sucks even harder? XML. If I can replace an XML configuration file with JSON then screw it.
https://stackoverflow.com/questions/48998034/does-toml-suppo...
General purpose until you need to store dates/times, self-referential structures, enums, etc.
json and toml are both serializations of data. I don't think the line you're trying to draw between "configuration file format" and "data structure format" is a well-defined line.
I wouldn't want to use if for config files or passing complex data but then it's not designed for that.
There is a distinction between markup languages e object notation languages. XML, HTML and Markdown are markup, JSON, YAML, TOML and most other are object notations.
(here my distinction, in general, is if they allow unquoted text)
It's even in the example file https://github.com/toml-lang/toml/blob/master/examples/examp...
I've heard people prefer JSON over XML because XML is too complex... and now we have json schemas, jsonpath... the namespaces can't be far away.
I'll admit that XML probably would be better without entities and the crazy whitespace handling.
In JSON they’re plain and simple. You even get to use good ‘ol [ ] delimiters.
In XML? It’s a sea of open and close tags.
Now do an array of arrays. Then hand-edit it.
Like I said, it’s no better. But it’s way easier to read.
In my experience, the biggest reason people hate it is because it has too many ways of structuring data, and it allows people who design the schemas to go overboard. Attributes or child nodes? Multiple namespaces or just extend the one you have? CDATA or embedding?
That said, simple XML is as good as simple JSON. XML is fine if you keep it simple when designing the schema. AND of course one can screw up with JSON too. But after almost 20 years of people over-engineering their XML schemas, I can't fault anyone for choosing a simpler data format.
Recently I've had to work with XML config files for a tool called Oozie, which is a data pipeline scheduling tool within the hadoop/spark ecosystem, and it has been soul crushingly tedious for me. Everything feels verbose and opaque, the documentation seems to prioritize enumerating every possible configuration option over providing minimum viable configs for common use cases.
JSON configs often just feel more simple and developer friendly. I'd say this has less to do with technical differences between JSON and XML and more to do with how "modern" software systems have been designed to be more ergonomic for developers/administrators and these modern systems happen to be more likely to use JSON.
It's also fun to hear non-technical people at work talk about "Jason files".
Ha! That one always makes me smile. One of the best exchanges I’ve heard was something like:
“... and we’ll have JSON handle all the serialization”
“Jason? Oh we hired a new engineer?”
I really wish everyone would think of JSON as a French word. "Jay-sonne" (or jay-SAWN). Not "Jase-in"... it would help keep the confusion to a minimum.
Ha - https://www.youtube.com/watch?v=zhVdWQWKRqM He starts by saying "Jason" but then suggests the French version. Love it!
I usually go the other way; Jasons I know have their names represented as "JSON" in my mind, and sometimes in written form.
To me it’s an improvement over JSON which is an improvement over XML.
XML has other problems though.
EDN is a better Lisp-flavored candidate, IMO.
I don't understand what this means.
The problem with XML itself is being unnecessarily verbose (and thus difficult for both human and machine to read) for what's just a way to encode trees. Attributes are arguably XML's self-inflicted gunshot wound in the foot; you mainly need them because of visual noise caused by regular nodes.
mapping a 1-1 s-expression translation on HTML/XML/JSON/YAML etc. solves nothing. YAML has (had) code execution security problem and parser incompatibility issues. HTML/XML actually need namespaces in a few cases. JSON is abused as "compile target".
There can be no one format that works for everything. This is why I like TOML, it is really good at what it tries to do and stops there.
<thing>
<optattr>42</optattr>
...
</thing>
insteadof <thing optattr="42">
...
</thing>
Attributes exist in order to create a more compact syntax within XML/HTML tags, in ironic recognition of these notations being horribly verbose.People can easily read and edit it with deeply nested structures, attributes, and even malformed data. Frontend frameworks using components and variations like JSX also maintain the same style because it's very natural to use.
Now try taking a typical HTML document and expressing it in JSON. Even an empty page would be completely unmanageable without tooling. People have a strange reaction when they hear "XML" but it's much more structured, usable, and widespread than you think.
JsonPath is the proposed standard, and it looks pretty much like XPath. And that's before getting into Json Schema.
HTML is not XML and even then few developers today use HTML without some sort of processing involved. HTML is also not a language used for configuration files, so it is really off-topic.
The question is, do people prefer XML configuration over JSON configuration. The answer is that people generally prefer JSON. JSON just maps far better to data types we use in everyday programming. Have you seen some of the common ways of expressing key-value pairs in XML? It's horrible.
> Now try taking a typical HTML document and expressing it in JSON.
That's not the problem anybody is trying to solve here. We're talking about configuration files, not complex documents. But let's say I wanted to solve that problem, the representation would look like this:
Not that bad at all.
In my world, config files are machine-parsed but human-generated and modified. JSON is just easier to work with.
Why is it so horrible? Have you ever typed a <ul><li> list? Or <input name="key" value="value">? What exactly is so difficult about this syntax in XML but completely fine in HTML?
The point isn't whether you can represent a document in JSON, it's about how easy it is for a human to manage it. The XML/HTML structure is far easier as things get more complex. The page you linked to shows this quite clearly even with the small "Bulleted List Example" at the bottom.
This still applies then
There's a lot of practical differences when actually implementing configuration with either XML or JSON, due to technical distinctions.
> The point isn't whether you can represent a document in JSON, it's about how easy it is for a human to manage it.
If XML is easier to manage, why are people shifting from XML and to JSON for configuration? You may have some experience with HTML, but have you actually used some of these XML abominations that are used for configuration?
> The XML/HTML structure is far easier as things get more complex.
Configuration files shouldn't be complex. They're mostly key-value pairs, perhaps a couple of lists here and there and a modest amount of nesting. XML is complex to begin with.
Perhaps at some stage, for some use-cases, XML starts becoming simpler to edit. Configuration files generally isn't one of them.
> The page you linked to shows this quite clearly even with the small "Bulleted List Example" at the bottom.
It's an example of representing XML-like structure with JSON, which is fairly easy. Try it the other way around, things get hairy. If you were to represent the data as JSON, you wouldn't write it like that.
I'm just not sure what the argument against this is other than verbosity. Config files shouldn't be complex? Sure, but what if they are? Are people really moving to JSON everywhere or is it just tied to the rise of Javascript and web frameworks?
Even if I would agree with this (which I don't), if it doesn't matter then you should pick JSON because it's easier to work with as a developer and it doesn't require XML parsing as a dependency.
> My evidence is how many people easily edit large complex document structures in HTML already without issue.
This isn't evidence at all. An HTML document is not a configuration file. Even then, people rarely author large HTML documents by hand these days. It's more likely that they're using a simpler language like Markdown to author documents.
> Are people really moving to JSON everywhere or is it just tied to the rise of Javascript and web frameworks?
If Javascript and web frameworks are responsible for the rise of JSON, but XML is better, why didn't they stick with XML? Remember, SOAP was XML. AJAX stands for "asynchronous Javascript and XML". The way to do an HTTP request (before fetch) from Javascript was called (inappropriately) XMLHTTPRequest. JSON on the other hand was just an informal spec with several flaws, yet it won out over XML.
2) JSON does need to be parsed and does add a dependency. Javascript is not the only language out there.
3) HTML not being config files is not the point. The document/tag structure is identical. If you can edit large complex HTML files (regardless of how they are generated) then you can edit large complex XML files. And it's rather easy to do so, against the claims that XML is so hard to work with.
4) There are plenty of XML configs out there, backing just about every piece of software you use. If you only focus on web/js projects then JSON "won out" because it is a simple dump of the in-memory representation rather than a more formal serialization like XML. The missing features like JsonPath and Json Schema are now being added back to turn it into a proper serialization format. When storing configs, these schema features are rather important.
2) A JSON parser is a far smaller and simpler dependency than an XML parser, especially when we're adding XPath to the mix.
3) XML isn't necessarily hard, it's tedious.
4) There's plenty of legacy software out there using all kinds of stuff for config. You don't see a lot of people choosing XML these days. You see YAML or TOML or JSON, even though all of these have issues of their own. JSON happens to be the simplest and most commonly supported.
Also HTML is not XML. HTML is non-strict (the spec is it’s parser not its format). HTML doesn’t have schema files. HTML doesn’t understand self-closing tags. HTML does have void elements (img, br, input, etc). HTML is designed for humans to write. And it’s a tool fit for its purpose, unlike the abomination that is XML.
Scroll down and I think it shows it perfectly how (X|HT)ML tags are much simpler than JSON syntax once things get complex and nested. It's not that hard to define basic primitives like we do with HTML (which has dozens of tags) but XML also lets you define your own schema if necessary to make things more compact.
I understand the technicalities between HTML vs XML but I don't see how it makes any practical difference when you're editing a bunch of tags in a text file. It's the same thing. The structure looks identical. What is the actual issue that makes HTML easy but XML hard?
Well, until you do need some metadata for that key-value pair. Which is why even in many JSON schemas it's pretty common to get something like { "key": "key", "value": "value", ... } (where ... is usually empty in practice).
The problem with MSBuild, for the most part, isn't XML - it's its own data model (which is not XDM, by the way).
My main point is in xml the closing tags are unnecessary, and you need an opening tag for every value where it’s obvious from nested context what that value is supposed to be. Xml is very redundant, there’s just all this zero entropy text everywhere which conveys no information. I disagree with your metadata in json example, why not just add more data to the value? The data model of msbuild is also annoying, but I’ve worked with some dotnet core projects now and using json as the project format definitely saves typing and actually allows you to intuit what a project is doing rather than just bombarding you with useless text.
Do you have difficulty in reading a large HTML document? That's as verbose and repetitive as XML but also usually filled with junk comments and malformed tags. If you don't find it hard, then why is XML different?
And with respect to data and metadata, what you propose is not fundamentally different in terms of verbosity:
<foo metadata="bar">baz</foo>
{
"foo": {"value": "baz", "metadata": "bar"}
}
And it's even worse if you also need to preserve order of keys.It’s just crazy to look at tbh, and it was annoying to type on my phone. I’m amazed people are still arguing in favor of it. In reality you just want the markup to transmit the most information in the least amount of bits. This is a measurable quantity and xml objectively sucks at it.
If that were the case, we'd be using binary serialization everywhere. But you yourself are making an argument that ease of writing matters. So does ease of reading. Overly verbose markup is a tax on both, but so is extreme brevity.
Anyway, this particular thread was a discussion to address a very specific point made in the article that overstated XML verbosity over JSON. I'm not actually arguing that XML is perfect, or even "good enough". Its syntax is overly verbose, and its data model has pointless distinctions and arbitrary restrictions. But it also had many good ideas, and it's unfortunate that those get ignored in the quest of simplifying everything - and then later, when the issues that were the original motivation for those ideas are rediscovered, that wheel gets reinvented in dozens of flawed and mutually incompatible ways.
const config = require('config.json');
console.log(config.apiKey);
With XML you often have to use things like XPATH etc. to extract the data which is a lot more effort.
I don't think people realize that JSON doesn't actually have a querying system at all, you have to deserialize it to an object to use. There is the coming JsonPath standard but that's not well supported yet and is pretty much the same as XPath.
But you’re not wrong. Yes you have to deserialise it. Hence it being an Object Notation system.
While you say JSON is trying to catch up with XML in regards to XPath, JSON is trying to catch up to XML with things like JSON Schema.
Absolutely. I would say XSD is the strongest advantage of XML over JSON right now. XSD still has many things to improve but JSON schema is even further behind.
You don't have to deserialize it to an object, you just do that because it's convenient in Javascript (and other dynamic language). It's a feature.
Now try using XML like an object, what do you get? A DOM. Which is fine if you wanted a DOM, but I don't want a DOM. I don't need XPath or any of that stuff. These are tools to deal with the complexity of XML, which I don't have, because I am using JSON.
So you're really saying that JSON deserializes to something that is a more natural fit for the language that you're using. And it's true in many cases; but also not so much in others, like when you're dealing with 64-bit integers, or dates, or all those other things that JSON doesn't spec because "complexity". In practice, it just means a proliferation of incompatible ways to represent these things, and utterly insane deserialization behavior in corner cases when implementations try to be "smart" to transparently compensate for JSON lacking something (e.g. https://github.com/JamesNK/Newtonsoft.Json/issues/862).
Conversely, if you are writing in a language that has integral support for XML - say, XQuery, or even VB.NET (https://docs.microsoft.com/en-us/dotnet/visual-basic/program...), the complexity is mostly not there. At the very least, if you control the format - which you have to, if you're in a position to decide what to use - then you can certainly stick to the subset of XML that is not anymore complex than JSON.
Sure, there are corner cases and limitations with JSON. I've never experienced that as a significant issue.
> Conversely, if you are writing in a language that has integral support for XML - say, XQuery, or even VB.NET...
I am not using any of that stuff, nor is there any reason for me to start using it.
> At the very least, if you control the format - which you have to, if you're in a position to decide what to use - then you can certainly stick to the subset of XML that is not anymore complex than JSON.
...or I can just use JSON.
XML (or rather, XDM, which is the appropriate level of abstraction to talk about this) has all those things:
https://www.w3.org/TR/xmlschema-2/#built-in-primitive-dataty...
Nor does it doesn't require an out-of-band schema - you can slap xsi:type on any element. And you can do that without breaking the data model, because namespaces keep data and metadata unambiguously separate, and code can easily deal with the former while being completely oblivious to the latter, unless it needs it.
JSON also has similar higher-level abstraction layers with more metadata. The problem is that nobody can agree on which one to use, or even whether to use one at all, and most code that's deserializing JSON in the wild is not going to be able to distinguish metadata from data.
Sure, you can add information until you arrive at a point where a string attribute or text content will be interpreted as a certain data type, but XML itself doesn't have it.
> JSON also has similar higher-level abstraction layers with more metadata.
JSON has all the basic data types built right in, there's no need for more metadata to do simple things. There's a reasonable mapping to basic data types and structures for almost any language.
> The problem is that nobody can agree on which one to use, or even whether to use one at all, and most code that's deserializing JSON in the wild is not going to be able to distinguish metadata from data.
...which is generally fine because of the aforementioned mapping. Your JSON library doesn't have to (and shouldn't) do any magic.
The only real advantage json has when it comes to deserialization is that it suggests to a human reader that keys are like object properties, leading to a very obvious deserialization strategy. But even that's a bit misleading, since json allows duplicate keys, just like xml, so a pragmatic deserialization library is going to make that feature impossible.
Make no mistake - the culture of straightforward deserialization is hugely valuable! But that's because of its history and other human factors more than the language itself.
Slightly less so as a JSON document, IMHO.
Cant do that with json.
But I'd posit that most - but not all - of the issues with xml config files have little to do with xml per se, and everything to do with the crazily detailed stuff people squeezed in it. You don't have to stick everything in a namespace, and nest even trivial things 3 deep with annotations for obvious types. And if you do those things in json it turns into a mess too.
SOAP is a perfect example of that. But: just because soap is messy doesn't imply xml everywhere has to be that.
But sure, xml has some downsides. Then again, so does json. Oh well!
Example is below where an application could either extract values at startup, or is extracted when code is built and deployed?
# My App
## Intro about My App
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
## Prod Server Settings
| Host | Port |Operating System| Type |
| -------------------------|:-------------:|:--------------:|--------|
| webserver1@myapp.com | 80 | Debian |Web |
| webserver2@myapp.com | 80 | Debian |Web |
| db@cloud.com | 1433 | aws |Database|
| webservice.com/api | 80 | aws |Database|
---------------------------------------------------------------------
## Support Information
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
## Application Roles
|Name | Active Directory Group |Description |
|-----------------------|------------------------|-----------------------------------------|
|System Administrator | MyApp.SysAdmins | Details who should be assigned the role.|
|Finance Administrator | MyApp.FinAdmin | " " |
|Finance Delegate | MyApp.FinDelegate | " " |
|Finance Analyst | MyApp.FinAnalyst | " " |
-------------------------------------------------------------------------------------------|I don't hate it, but I really dislike it. It's definitely better than JSON for this purpose, but it's still quirky and a bit of a pain.
One example from a larger file:
networks:
macvlan51:
driver: macvlan
driver_opts:
parent: eth0.51
ipam:
config:
- gateway: 192.168.51.1
subnet: 192.168.51.0/24
Maybe I'm just getting older and dumber, but that hyphen under config seems completely arbitrary to me (and the necessity of it is poorly documented in the manual). This gets magnified to the extreme as your config file grows. Maybe this is a list of objects under a key? Who knows.Edit: OK, it looks like hyphens indicate arrays in the schema but for me it feels like a guess as to which one of these to use for each particular case.
I’ve been editing long sublime-syntax files recently and I do not like it much. But I’m also not sure if JSON would be better for such a thing.
I do know about a number of gotchas, and stepped on some briefly. Yaml is not ideal, but its set of tradeoffs is more suitable for human-editable configs, from my point of view.
It doesn’t matter the language or syntax, if the unit of structure doesn’t fit on the screen, it’s hard to work with.
But comments are often code smell—a crutch for poor identifier naming and design. If your names and design are well thought out you rarely need comments and the consumer of your code should be able to understand it using the statically checked source alone—-versus manually having to parse your unchecked human language documentation.
Sure, but how often does that happen?
Use as explanation of intent.
Use as clarification of code.
Use as warning of consequences.
Here's a link to a well-known recent book, half of which is pretty much about why those points are a real and frequent thing, and why code should be much more thoroughly commented than it's recommended by the usual philosophies.https://www.amazon.com/Philosophy-Software-Design-John-Ouste...
Comments are important when they express things that code cannot.
Examples:
// See ticket XYZ-123 for discussion and rationale.
// We are sidestepping a bug in Docker 3.x here.
// TODO: Allow queue size per service version. Currently we're using max size for all.
["DoStuff", "EvenMore"/, "ops go back :("/]
But yes, if every implementation of JSON moved on to JSON5, I don't think we'd be having this discussion.
I didn't. There wasn't much to the blog post. In fact, the blog musings are the same criticism we've heard a thousand times. But please don't suggest that I missed some deep point in a trivial blog post when you really just mean "I disagree".
I even pointed out a real-world concrete example where JSON config is made pleasant to edit by hand for humans: an IDE like VS Code that pulls in JSON schema.
Though since you only doubled down on the blog post's opinion instead of responding to a single point I made, can I say that you missed the points of my comment? You didn't even address my criticism by pitching an alternative. How are we supposed to have a conversation?
I think the debate here is what "editable by humans" means. If you're using a tool for editing JSON like VS Code, I don't believe you're really editing it by hand. You're using a program that has specific things for JSON that give you to have a pleasant experience editing. I believe editing by hand means using a standard plain text editor and it's a lot worse.
Formats that use braces are relatively easy to parse by machines but can be harder to read by humans. Utilizing white space makes things easier for humans to read. A JSON file could have everything on one line. But something like YAML it forces you to use white space and new lines which helps humans to read it.
Still, editing JSON directly by an IDE that still exposes its syntax is "editing by hand", just as editing C in Vim with ctags is still editing by hand.
I do have to note though that VSCode uses JSON with extensions. Most obviously, it allows comments. But there are other minor things that make it easier to write (and parse) - e.g. it allows trailing commas in literals.
It's not people to have to worry about. It's the machine generated code. But now that I think about it more, YAML is a bad example since it's a superset of JSON. So a machine could generate really ugly YAML too.
The only significant thing missing from JSON is comments. JSON5 or convention can solve that, so it's not as bad as you'd expect.
I have never understood this argument. I'm not saying it's the best, but I find it much much more readable than yml or xml.
some_collection:
- first_entry
vs some_collection:
first_entry
When you're staring at a non-trivial YAML file, it's very hard to spot these errors, and you'll potentially get some very weird behavior in whatever is depending on the config.The use of brackets means if something is broken, you'll know explicitly because your IDE will throw a fit. Formats that are white-space dependent can have subtle errors that aren't easily recognizable on first glance.
The 2 problems with JSON are multiline string support and comment support, but JSON5 solves both of those problems. Sure JSON5 isn't JSON, but YAML isn't JSON either. No matter what you're going to need a library to parse a config file, so why not stick to something based on a widely used data-storage format?
{ useFluxCapacitorComment: "Enables time travel. true or false", useFluxCapacitor: true }
I don't mind the formatting, but I'm a professional developer, so my tolerance for complex UIs is abnormal.
Written on mobile, so that JSON may look like garbage.
E.g: npm/yarn will complain loudly if you add unexpected lines in package.json IIRC.
I can at least imagine such a configuration file format could be both simple enough for human and machine to reason about, and flexible enough to add value over (say) json. But the devil is in the details; and it's not always immediately obvious what's too complex.
In any case, json5 looks like what json should have been from day 1; several of the really critical improvements were common JS practice when json was invented.
I'm a little worried about the IdentifierName extension, since that's more than what javascript itself allows (no reserved keywords!).
But json5 is also an interoperability problem. The nice thing about a (good) standard is the that if it works someplace, it'll work anywhere. Add json5 into the mix, and that's not the case anymore.
Then again, there aren't a huge number of plausible successors to json, so maybe this is the best we can hope for. And it's trivial to down-convert to plain json, which means it's not too bad, I guess?
I wish json5 simply worked everywhere ;-).
https://github.com/lightbend/config/blob/master/HOCON.md#goa...
TOML is fine.
shivers
> You can't even figure out what the dependencies are going to be for some Gradle projects without executing them
This sounds like an issue of Gradle, not of programmable configs in general.
Not every config file needs that, but a config file for a build system definitely needs that. Otherwise your build system becomes too restricted and you must either write your logic in plugins or use another scripts along with your main build tool. Both options are bad.
I'm not even sure that build.gradle should be called a config file. It's a build script.
Originally, the whole point of using configuration files for builds was to eliminate the programmatic element where you used lots of little scripts to run builds. Config files are easy to read and reason about.
> Programmatic configuration allows e.g. specify some library version once (e.g. springVersion) and use it for 10 dependencies as a variable
Declarative syntax can also include variables which can't be reassigned to later.
Gradle's original marketing message was based around its syntax, i.e. Apache Groovy being easier to read than XML, which is true. But providing the programmatic ability of Groovy as well is a step backwards for configuration best practice, ultimately resulting in an increase in technical debt. Thankfully, almost all the Gradle build scripts out there are simple 20-liners which don't use programmatic logic.
Gradle ought to follow the example of Jenkins pipelines, which later provided a Declarative Syntax as an alternative to Groovy to mitigate this very issue.
For the same reason that I would want to figure out if my script type-checks without executing it - making it easier to avoid bugs and reason about the program.
if you ever have a non-programmable config file, then at some point someone will make a turing-complete programming language which goes on top of your tool.
I wrote this after getting annoyed with MediaWiki's new extension format which uses JSON files to set up the extension. What I wanted to do is not include some CSS if a certain plugin is loaded, and that's pretty much impossible to do in a declarative configuration file. Using a JSON file – or any other declarative config – to describe how code works is not a good idea IMHO (although it's probably fine to add basic metadata).
The worst case of declarative programming gone wrong that I've seen is probably k8s.
I can't really judge your Gradle example, as I've never worked with Gradle (or Java in general).
> I'm getting a bit fatigued of these you're-doing-it-wrong posts
I agree, and I would rename the article if I hadn't put the full title in the URL (I have since stopped doing that exactly because I wanted to rename this). In my defence, at the time I was annoyed with MediaWiki, PHP, this JSON stuff, and the reasons I even had to work on MediaWiki in the first place (which is a long story involving a lot of drama).
That being said, I think using JSON as a human-editable configuration format is, quite frankly, never a good application of the tool. I can't think of any cases where it's the best tool, chiefly due the lack of comments, but also because it's kind of a pain to write. Variants which allow comments – such as JSON5 – are okay, but that's no longer JSON.
True... for end users, but absolutely not for other developers. I often see this mentality used as a crutch to baby developers from having to write original code.
JSON5 makes things better, but it still requires too much syntactical noise and multi line string values are still not there.
I don't understand how anyone who ever used gradle or even maven and went somewhere else can actually say that
Gradle is simply amazing and literally don't know what could be better about it. The link posted is so trivial compared to it's power, it's like criticizing a bullet train for the pattering on the seats.
IMHO JSON5 (JSON with comments basically) is fine for config files, VS Code is using this and they seem to be doing alright.
Addressing the article's points:
- Lack of comments: not an issue with JSON5.
- Readability: I think it's pretty readable. Are the quotes, semicolons and commas really an issue? Not for me.
- Strictness: just edit it with an editor that points at the errors quickly. Escaping quotes might be annoying, but I can't think of a possible alternative format where escaping isn't required at all.
- Lack of programmability: I've never had any problem with this, and again pretty complex and customizable applications like VS Code seem to be doing fine without this. This may even be considered a feature, as it forces you to keep the configuration well separated from the actual logic.
TOML is my personal favorite.
YAML feels like 10 steps back; no parser I've ever used has been able to accurately give me a line number for an error, white space issues make it impossible to work with without special editor features, and some of the types are just cryptic. I spend most of my time that's spent editing configs in vim over SSH and it can be challenging at times.
TOML resolves a lot of these issues in my experience but I'd still argue that by virtue of its capability, is still quite complex when trying to define configs for new applications.
The only complaint is that the name TOML isn't as cool as other formats.
When trying out a CLI I've been writing recently, this time on Windows... the TOML config file (generated on first run) wasn't usable.
The problem turned out to be TOML not handling single slashes "\" in strings, instead treating them as escape characters.
Annoying as on Windows the paths are (eg): "C:\Program Files\stuff". Which then needs changing to "C:\\Program Files\\stuff" to work ok.
I can see both good and bad points of TOML's string handling there. But it's annoying it needs a workaround for one of the signature aspects (slash vs backslash) of windows. Especially when paths are very common in config files, regardless of OS. :/
TOML has two types of strings, each of which can be single line or multi line [0]. Basic strings allow escape characters, while literal strings do not (so there's no way to represent an apostrophe in a single line literal string). Multi line strings are like Markdown's code blocks:
"""
This is a multi-
line basic string.
C:\\Program Files\\stuff
"""
'''
This is a multi-
line literal string.
C:\Program Files\stuff
'''
[0] https://github.com/toml-lang/toml/blob/master/README.md#stri...Seems like YAML won.
But TOML seems to be heavily used in Rust.
Also there is SANE.
Yeah, but now you have to find a tool that supports that. Easy on small projects, but try being at a company supporting many languages and frameworks.
> - Readability: I think it's pretty readable. Are the quotes, semicolons and commas really an issue? Not for me.
If you haven't tried TOML, you're missing out. Why on earth would you put up with syntax (hard to author, at that) that isn't necessary? Syntax distracts from the actual problem that this is trying to solve. JSON is fine for machine read/write, but horrible on humans.
> - Strictness: just edit it with an editor that points at the errors quickly. Escaping quotes might be annoying, but I can't think of a possible alternative format where escaping isn't required at all.
What if you have an issue in prod and need to fix something quick? You might only be able to open it up in a stripped-down vi, or if the machine is locked down you might only be able to materialize a plain text view. What if not everyone uses the same tool? This imposes a further constraint. Yet more concessions pointing to how horrible JSON is for humans.
> - Lack of programmability: I've never had any problem with this, and again pretty complex and customizable applications like VS Code seem to be doing fine without this.
Can you imagine projects where this might be an issue? These aren't the only codebases in the world. And just because VSCode appears to be doing fine doesn't mean they don't second guess that decision. They might just be locked in at this point.
- static config
- environment specification
- runtime feature flagging
- semi-static control plane config
- rollout weights
Etc.All of these have different characteristics.
Infrequently changed, a priori config that doesn't need to be changed in emergencies is suitable for baking in at compile time.
TOML is decent but if you need nested data it gets super awkward.
> What if you have an issue in prod and need to fix something quick? You might only be able to open it up in a stripped-down vi, or if the machine is locked down you might only be able to materialize a plain text view. What if not everyone uses the same tool? This imposes a further constraint. Yet more concessions pointing to how horrible JSON is for humans.
JSON5 is pretty easy to edit correctly with any text editor. Not much harder than TOML.
> Can you imagine projects where [lack of programmability] might be an issue? These aren't the only codebases in the world. And just because VSCode appears to be doing fine doesn't mean they don't second guess that decision. They might just be locked in at this point.
Does TOML allow programmability that JSON5 doesn't?
And if you're editing a file, you can usually tell by the fact that there are comments in it. :)
Just fix it quick somewhere that’s not prod. Snowflaking has not been a good practice for a long time now, so arguing that a certain tool makes it easier is a bit of a moot point.
There are trade offs with the various formats. Think about your use cases and pick the one that fits them the best.
Thanks for pointing me to that - I'm working in Scala on server-side and JS on browser-side for a couple of years now, and sharing config would be nice. Still need object merging and value concatenation, which hocon-js doesn't support, but I'll keep an eye on it!
${service}.${env}.example.comLink for HJSON: https://hjson.org/
Compare to JSON5: https://json5.org/
Because it was designed to be a human-editable configuration file format, it has a simple and intuitive format that is easy for both humans and machines:
[Section]
; comment
Key1=Value
; comment
Key2=Value
[Section2]
Key1=Value
It is also easy for machines to non-destructively edit, maintaining existing comments and formatting by humans, and applications take advantage of that. That is a big plus in my opinion.Good for: comments, readability if keys limited, familiarity.
Comments might work for a parser but not on another. What's the point when you can do better with TOML?
A sqlite file isn't an ascii text file that you can edit with vi, so that suggestion is likely to get a lot of jeering, but it's a serious suggestion. It's already being used for configuration on all mobile platforms. There exists a wide range of applications, particularly for windows users, where the common user is not a developer and is not meant to be editing configuration by hand.
Furthermore, access to the sqlite3 binary is very nearly as widespread as access to a vi binary. And self-styled developers who don't know SQL aside, it actually is a rather convenient format to edit by hand. Machine transformation of the config is obviously straight forward, and sqlite databases tend to be self-documenting anyway. Sane table and row names go a long way (obfuscated tables and rows would be roughly equivalent to obfuscated keys in an ini file...) and if that's not enough, comments for tables and rows can be stored in the schema.
The one downside is strictness; sqlite doesn't enforce types. It does however have data constraints, which help.
There are formats that are easier to edit than others and formats that are easier to parse than others. But sooner or later all of them end up being created by an ill-suited template engine or embedding a programming language in the syntax (cloudformation, ansible, hcl2).
Why not treat the config file as a generated artifact that is readable but not written by a human. Tools like jsonnet (https://jsonnet.org/), dhall-lang (https://dhall-lang.org/), or my personal side project ucg (https://ucg.marzhillstudios.com/) allow you reuse values and safely generate your configuration formats with comments and logic all while bypassing the pitfalls of most formats.
They give you a purpose built language for creating configurations with comments and logic w/o putting that burden on your application later. And it allows you to ignore the parts of a particular format that cause you problems.
It's just a config file, no need to overthink this with translation layers IMO.
If you only have one application then it's overkill. When you have 10's of applications it's a life saver.
I think this makes sense as far as it goes.
The first choice, no matter what the config file format, should be editing within a tool. The tool can check values, be accessible, assist in coordination, etc.
However, I think it's essential that the intermediate format be human readable and have the ability to contain comments. Because if something in the toolchain goes wrong, determining what the settings are right now on a particular system will be very useful. Being able to modify those settings by hand, though not your first choice, may save time and money and (depending on the system) lives.
Also, if the tool breaks, you can still change configs (though obviously this is not what you want).
Essentially, you should have a tool that edits config values with full explanations of their purpose. That tool should output a machine-readable format (JSON5, YAML, TOML) that supports comments with each value preceded with the comments containing their explanations and, ideally, a list of comments made when changing config values with previous values listed.
You get the best of "both worlds."
One nifty way of doing this is custom quote sequence.
In C++ it works like R"delimiter( raw_characters )delimiter"
As delimiter can be anything it can be chosen not to conflict with the payload.
Bash has had the same trick for here documents for a long while.
HEREDOC-like format:
#qweqwe123" string where everything is unquoted and is terminated by a " followed by qweqwe123. you can chose a short relevant sequence of alphanumeric chars as needed"qweqwe123#
I am the author. It depends on your purpose; in general, I would say "the simplest solution that meets all needs", where "all needs" depends on what you're doing. In Python, this could be the configparser module, or parsing a Python file (again, depending on requirements).
As a matter of taste, I prefer minimal syntax. I wrote a library with this syntax a while ago[1], although it's clearly not the best solution for every case.
I should probably write a separate article about this.
[1]: https://github.com/carpetsmoker/sconfig#what-does-it-look-li...
> Lack of comments: not an issue with JSON5.
JSON5 isn't JSON though. It's JSON5. None of the standard JSON parsers can deal with JSON5, you need a different JSON5 parser.
> I think it's pretty readable
A matter of taste.
> just edit it with an editor that points at the errors quickly
It seems to me that having to rely on such tools for issues such as this is not a great idea, especially not if it's easy to do better.
> I can't think of a possible alternative format where escaping isn't required at all.
There is a difference between "escaping isn't required at all" vs. "having to constantly and awkwardly escape a commonly used character".
> Lack of programmability [..] VS Code seem to be doing fine without this.
I've not used VSCode, As I'm a Vim guy. I can't imagine configuring Vim with just declarative configuration as so many useful parts come from being able to program Vim. This could be separated out to just an "extension" or "plugin" system, but I really like being able to just stick one or two lines of code in my vimrc and have it do something really useful, without writing a full plugin.
How about "J4X" (JSON for XML), that lets you embed XML in JSON the same way the ever-popular "E4X" (ECMAScript for XML) lets you embed XML in ECMAScript?
https://developer.mozilla.org/en-US/docs/Archive/Web/E4X
J4X Example:
<!DOCTYPE j4x PUBLIC "-//W3C//DTD J4X 1.0 Transitional//EN" "http://www.w3.org/TR/j4x/DTD/j4x-1.0-transitional.dtd">
{
april: <fools late="13 days"/> <!-- sorry! -->
}Many years ago I worked at a job that made an XML tag-based language that was turing complete with the intent of it making their configuration "smarter". It ended up making the entire project much more difficult to debug, trace through, and even understand at all.
Please don’t. Ever. Not even once. It’s a really bad idea.
Followed by a list of reasons of which lack of comments is the only one with any universal weight.You make it sound like it's easy to just "propose an alternative", but it's not really. First off, some problems aren't necessarily obvious from just reading a few examples. YAML is a good example of this with a lot of subtle behaviour that can really trip people up. This isn't really obvious form just a glance, and required an in-depth review. So doing a full in-depth review of all alternatives is much harder than it might seem.
Secondly, there is no single right answer. It depends on what you're doing, the language and environment you're using, social factors (e.g. if all tools in your company already use XML, then it's probably a good idea to stick with that), and probably a bunch of other factors.
Alternatives that are better are also so plentiful that quite frankly, listing all possible alternatives strikes me as rather redundant. YAML, TOML, JSON5, or even eval(), and many more, all are decent alternatives for many different scenarios, although obviously not all.
That JSON is popular doesn't mean it's a good idea. I think there is a simple reason that it's so popular: for quite a few programming languages it's the only file format that's supported in the stdlib, and people are already kinda familiar with it. This is not unreasonable choice at face value, but at the same time a lot of people don't always see the downsides at face value.
"Tell me what I should do, not what I shouldn't do" sounds like a half-wisdom. Sure, it's probably more useful. But at the same time, pointing out possible errors people haven't thought of isn't useless. I've actually received quite a few emails over the years with the gist of "I wanted to use JSON for my program, but then I read your article and decided it was a bad idea so I used YAML instead, thanks!"; so clearly, it's useful for some.
Your stove metaphor breaks because the alternative of doing nothing have no consequences.
Blogs are meant to offer insight and arguments, and all the top level comment is saying is that this blog did a bad job in that respect.
I do wish though that JSON5 would become more widespread, as that basically solves every downside of JSON.
Now, if you add programmability to your configuration files... that is asking for someone to abuse it and turn a simple configuration file into an abomination. I've definitely seen that happen.
Back on topic, first we’re not supposed to use text files. Then, no XML files. Then, don’t use YAML. Now, we’re supposed to stop using JSON.
At some point you realize the problem isn’t with the tech but with the programmer. We have this need to constantly reinvent the wheel all the time. It’s silly.
OP's argument is silly. JSON files are fine for config.
The same reasons you'd want comments anywhere else. As one example, often in a package.json file I want to add a comment that a dependency is intentionally using an older version due to an incompatibility. The workaround is to use a key of "//", which is hideous: https://github.com/npm/npm/issues/4482
Username? Got it. Password? OK. Hostname? Sure. Why do these things need comments?
If there's subtleties to the config, put that in the README. Or give better names to variables.
Functions are not always self-explanatory and get complicated fast, so comments in programming make a lot of sense and are helpful. A config file in theory should just be a series of assignment variables-- it's not clear to me why these require detailed explanations.
Things like listen on port 8081 instead of port 80, because the loadbalancer does something weird. Or this strange config is important because of feature X and blah. Or if you change this here, be sure to change it there.
You're conflating two things, the readability of very large config files, and the readability of config files that allow comments.
> Good luck finding those 3 lines you need.
Personally, since I live in a world where searching text in a file is commonplace and trivial, additional text to match, possibly even comments that note where the config differs from the stock one that ships, are useful.
Yes, searching Apache config is pretty much the only reasonable way of finding what you need. But have we really gotten to the point where we passively accept gargantuan, unreadable config files because it's possible to search for the exact pattern you need?
What if there's a problem in your config and you need to find the issue? Prepare to set aside a few hours of your time to read every block.
For stock configs that ship with a lot of comments, I would sometimes replace the file with one where I've stripped the comments. Since I always save the stock config as $FILE.dist, I get maximum usability. Never would I want the configs to no support comments though. Even after I've stripped configs, I might add my own comment to each change from the stock config, or for some test change I'm trying out. Not having comments means any documentation would need to be at least one degree removed from the item it's documenting. Sometimes that's acceptable, sometimes it's not.
# Please don't edit this file directly (it is updated with every Red Hat
# Linux update, overwriting your changes). Instead, edit /etc/...
Nice. I now know to make changes in a file that won't be overwritten by an update, and if there are issues, it's either with the site local file, or the one provided by Red Hat, and it's probably (although not exclusively) the site local file that is in error.He does explain, there is even an example.
How about putting instructions in the instruction file (README) and configuration data in the config file?
When you build a file from scratch, or delete the embedded documentation as you go, you end up with reasonably compact configuration.
Additionally, apache has like thousands of options because it's software built to adapt to the world, and not software built for the world to adapt to it, so that adds to length.
There are many cases you need an occasional comment: to explain why a possible surprising value is set to what it is, for example. Or to warn future editors for a mistake you made in the past, or even to just describe what a potentially confusing setting does.
https://github.com/dhall-lang/dhall-lang
Dhall gives you the power of a limited programming language, the power to version configs and perform generation and logic in your configs, gives you the ability to spread your configs and custom functions for config generation across multiple machines safely, and does so with good guarantees about the type system and does a good job of stopping potentially infinite loops.
Dhall is really, really good and very overlooked by the industry just because its syntax is functional.
I've been rocking the ini file config for 25+ years, it just works, zaro bugs, and I can get on with building cool stuff.
Yes, you have to include the runtime, but the size of it is all but guaranteed to be smaller than whatever library you need to parse YAML or TOML or XML (or JSON in many languages.) Smaller than some fonts.
The syntax is almost the same as JSON, but you can use comments and multiline strings. There are also APIs for just about every language imaginable.
You do sacrifice an enforced lack of programmability unless you're self disciplined, though, and Lua does index at 1 instead of 0, which is annoying and objectively wrong, but still less of a gotcha than some of the crazy stuff YAML does.
... that said, I do also feel like anything touching the web stack at all should probably be using JSON just because javascript engines already parse it.
Configuration is in Lua's roots. It is specifically designed for use as a self-documenting configuration language.
I think the issue is that people just don't know how to use Lua to load config files. ;) But the fact is, there are few other better options than to include the Lua VM and then put all configuration management in .lua files on the target system.
As for the index issue, you might find it annoying, but I would argue the "objectively wrong" bit (there are other languages, like Fortran, that use 1-indexing).
I consider that significantly worse. With JSON, you at least can assume that the person reading it will understand what the file means. With a proprietary format, especially the popular "key=value" format, it gets super murky - what are the rules for quotes in this dialect (are quotes required, optional, or interpreted as part of the value, which escape characters), will white space be included, etc.
Lack of comments does seem like a very valid point.
I don't see the issue with readability. It is not great but still OK without syntax highlighting, and superb with highlighting (which works because it's a wide-spread standard format, not some weird custom dialect).
The strictness is mildly annoying, but violating it will make the config obviously invalid, which means it can be rejected instead of silently breaking production because you used a single quote and this dialect's parser only considers double-quotes, so your single quote was considered part of the string.
I don't know of many projects that use programmable configs, and many attempts to achieve this resulted in horrendous nightmares.
Seems like a superset of JSON that remains a subset of JS (like JSON5) can address most concerns, and can still be syntax-highligted with a JS highlighter.
Sorry, that’s nonsense. JSON is young.
We lived happily with many easy to read & parse file types before JSON arrived. Everything has changed because CPUs are fast now (so parsing overhead isn’t noticed) and storage isn’t worried about (JSON is bloated).
I say too late because there's no migrating the world's json parsers at this point. We could decide to all agree on a schema and comment format that uses the existing syntax with "#comment" and "#schema" fields but there doesn't seem to be a defacto standard.
To me the simplest and the easiest to validate used to be the old good INI but unfortunately it never got its chance to be strictly standardized to support nested structures. JSON is the next readable/validatable but it is slightly worse in all respects.
That is a feature! If you need to "nest" structures, you are not really writing a configuration file.
So I can't see why JSON would be a bad choice either.
There are some things in EDN missing in JSON, richer types, can be extended by the user and support for comments mostly. I'm not sure if not having those would be a deal breaker though.
I've used YAML for some things, and I haven't found it better. In fact, it's annoying to have a different syntax for your config then you do for your code. And the whitespace matters definitly has been a source of frustration.
JSON is plenty readable for most in my experience and there are plenty of great readers/prettifiers for it.
Having flexible configuration files sounds like a terrible idea. I don't want my mistakes to fail silently. Not to mention that the only problem the author mentions is trailing commas...
as mentioned better by others, lack of programmability is a feature, not a bug. Configuration files are presets and markers, not functionality.
I don’t want to program my config files, it goes against the whole idea of configuring stuff.
JSON being JSON makes it language agnostic and easily shared (via APIs and whatnot).
And as for the changes argument - try inline commit log in a file editor (eg GitLens in VS Code). Not perfect, but it does give you some history at a glance.
(The point above is weakened by JSON’s lack of dangling comma support, so one-line changes often affect two lines).
PS They do have "comment" fields that record when the file was created. I don't find it "just damn ugly" at all.
I added comments, a way to have node names that don't require quotes, hex constants, base64 blobs etc etc and even have annotated nodes (for translation and that sort of things).
I know it's not purist, but I never tried to shove it down people throats, it just fits my use case(s)
Is there a way to have comments, such that when the file is read and then later written (by the program), the comments are preserved (even if some of the fields changed)?
it has no comma issues (commas are whitespace), it has comments, it's very structured, it got better set of primitive types and literals for them, it has native support for tagged values for encoding more complex types, it's got first class support of encoding computation by virtue of being subset of clojure - lists can represent function calls, the usual lisp shenanigans, it has readability of yaml and none of it's drawbacks.
it's a shame EDN is not more widely used.
you've just described strings, shall we get rid of them too? :)
i find it weird that you would complain about presence of some feature which in no way interferes with any other features and can be completely ignored without any consequences. was there something else you wanted to use parens for in EDN serialization format?
But I don't consider it a major deficiency, just a nit. I mean, in the comment that you've responded to, I literally said that it's one of the best contenders for a human-readable serialization format today - it's certainly much better than JSON, or XML for that matter. It's just not perfect.
json5 to the rescue: https://json5.org/
> It’s just not that readable. Sure, it’s readable for a data-interchange format, but not readable for a configuration file.
I'm of the opposite opinion. I find json to be the easiest to read, as opposed to yml and xml.
If that’s not an option I’d prefer xml (with some sadness). JSON is a bad idea for the reasons described in the article, and YAML is too clever for its own good and is definitely a loaded foot-gun.
I think that this library found a very good compromise between configuration and programmability.
In configuration files you want overrides, includes, comments and inheritance. May be very very simple pattern matching.
Yet, you do not want flow control structures such as if-statements/loops & gotos.
Also fully convertible to json (without comments) if there is ever a need to change format or to interface with another system that can only understand json.
JSON5 is new since that was written I think... there are a bunch more of them too.
As part of the team at lightbend that came up with HOCON I still like it because it lets you use env variables (handy for Docker, Heroku, etc) and has some don’t-repeat-yourself capability without going to a full programming language.
(If you’re going full language, to me why not use a real one, maybe the one your app is written in... but there are definite tradeoffs to using a language, like difficulty writing automation that understands the config, and it assumes anyone changing config is a full-blown developer)
What in god's name is that??
I persuaded the author of Linux Libertine to add the first two as "historical" alternatives.
https://english.stackexchange.com/questions/25118/is-there-a...
Really?
Depends on how it’s structured, but I’d say XML is much more READABLE than JSON, although it requires much more typing to write by hand.
And it was a much more rigorous data format than JSON for many years before any of the more modern things like JSON schemas came about.
XML can be programmable. You can make <If> and <Loop> constructs if you so desired while being trivially parse-able.
XML can be arbitrarily strongly typed.
JSON really was a step backwards in many ways. I think the main reason JSON took over was that it looks nicer and is easier to type by hand (as if everyone doesn't have editor macros for XML).
Actually I consider that a feature. If some piece of software requires some complex configuration I have to refer to some external documentation anyway. Many projects include the documentation (or parts) as comments in their configuration files (e.g. Postgres' pg_hba.conf), but I think it makes it harder to find the interesting parts.
> Readability
I think JSON is pretty readable, even for non-programmers, but this is highly subjective.
> Strictness
Good! Parsers are too lax anyway.
> Lack of programmability
Again, that's a feature: just keep it simple. Now JSON is not perfect (I would like to have a distinction between integers and floats), but it gets a lot right and it supports the most important data types and structures
- strings
- numbers
- dictionaries
- lists
that easily map to data structures found in higher level programming languages.Except something seemingly simple which doesn't attract your attention might have a weird reason for existing - for instance you might have dependency which has to be pinned to a certain version due to compatibility issue or an existing bug. Would you rather spend 2 hours troubleshooting that or maybe put a comment there so that the next person can easily understand the core issue and evaluate it in 5 minutes?
Where do you document you use of that external document? In a configuration file the support comments, you can do it in a comment:
# See Necronomicon, page 751 to determine these parametersThe fix was easy enough---change to a nonstandard port. But sans comment, there's no indication why we were using a nonstandard port. With a comment, the ops guys know why we're using a nonstandard port, and even ask "is that still an issue?"
Edit: clarify why we did the workaround we did.
HCL and HCL2 from hashicorp have a similar feel -- which makes sense, since they were inspired by ucl and nginx configs apparently (so says the readme).
YAML has dangerous type coercions and tedious indenting.
TOML has bizarre formatting for arrays and associative arrays.
And other formats have too-small adoption.
We know that every config format is bad, but we appear unable to correct this bone-headed simple problem.
languages:
- en
- de
- no
portmappings:
- 8080:80
- 2222:22
parsing the above, "no" is suddenly a boolean, and YAML 1.1 parsers will turn "2222:22" into the number 133342 (because there was a helpful rule that thinks the latter means "2222 minutes 22 seconds" and turns it into the number of seconds... https://docs.docker.com/compose/compose-file/compose-file-v2... )I really liked it initially, but after using it a bunch I'd prefer a more restricted subset of it. (also hit a bunch of parser bugs/incompatibilities, where the complexity of supported cases probably also plays a role)
JSON is great if I'm going to want to generate configs programmatically much of the time but still want to be able to view or edit them manually now and then. JSON (+comments) is a fine config format for vscode since it's (among other things) a specialized JSON editor, and it gets to leverage functionality it already has (JSON schema support) towards making it's configuration management pleasant.
If the config is going to go in a users home directory, like ssh or fontconfig, or for services like nginx and polkit where other software is going to want to add some configuration, then support for config glob directories (e.g. conf.d) is very handy, so that different system packages or gnu stow directories can provide their config via file tree merging.
Some software gets a ton of value from using a scripting language as it's config format, although in general it's something to avoid. Vim and emacs wouldn't be what they are with a less powerful config format.
Some programs need just a tiny sprinkle of config, just to specify the socket to bind on and maybe some database credentials. INI files or even just environment variables are ideal for this sort of thing.
JSON has its place, the problem is that it's too easy to use. Programmers automatically turn to it without thinking about what their software needs from its config format. It's the not thinking that's the problem.
I did a thing that used JSON for config years ago when I was young and stupid (60). Terrible choice. Someone in this thread said that config files need the amenities of source code. I agree. However it also needs to be readily machine generated in case your system is successful enough to support a UI for configuration.
.ini hits the sweet spot for me. I can edit it reliably. It's extensible in every way I've ever wanted.
I looked at TOML, mentioned below. I would like the array syntax but when I saw the Wikipedia article's example including something with quotation marks I realized that complicating it with array syntax means that you hop down the slippery slope to having semantic cruft to distinguish the types.
For .ini, everything is a string, period. You always know what you have. No debugging.
In fact, I wrote myself a .ini interpreter that does some obvious stuff to make it useful in Javascript. Case-insensitive interpretation of true/false. Things that can be a number become one.
But then, slippery slopes galore. I added hinting to let me convert objects with number property names to arrays. Now I spend my entire life realizing that I didn't add the hint for this recently, quickly added case.
I'd be better off just always converting it where I need to use it.
It's my opinion that changing anything about the .ini is always premature optimization.
I haven't heard about JSON5 before now, and I don't know what is JSON5 (I read in here apparently JSON5 is JSON with comments; what format of comments (is it same as JavaScript), and does it accept trailing commas?). Also, XML is OK for markup, but it is often used for other stuff where other formats are often better (although maybe you have some case where the other options would be worse than XML for the configuration format).
It's pretty amazing and I'd recommend it.
Isn't json a subset of json5? If so, but write out to json when serializing, but always read json5 so you never run into any surprise? Any drawback with this approach?
While JSON5 doesn't introduce huge changes as such, it does make it more complex. There is also the matter of semantics/API; should json.parse() accept JSON, JSON5, or both? What if we expect JSON and someone sends JSON5; should we accept that? Postel's law says yes, but personally I think that would be a mistake.
It seems to me that it's easier to maintain a clear distinction between the two (json.parse(), json5.parse(), etc.) One of the values of JSON as a data interchange format is that it's fairly simple, and adding JSON5 features – none of which are needed for this usage – probably isn't a good idea.
Also note that there are many JSON supersets. YAML is a JSON superset: any valid JSON is also valid YAML, there's alos HOCON, and probably a few other things as well.
Randy's law 637: Every configuration file format (read by some language 'L1') eventually gets control of flow, assignment and statements. This format then forms language 'L2'.
Proof: See Unix.
This is like replying with "but a changed version of this beer tastes great" to "I don't like this beer". Okay, but that's not what I'm drinking.
The author says something like "__comment" is ugly for humans editing the file. Sure, it is. But it works much better if the file is also going to be edited by a UI. (Although "__comment" can comment the object but not an individual key; something like "name#comment" being comment on "name" key and "version#comment" being comment on the "version" key could permit separate comments on each key.)
I actually am encountering a similar problem at work right now. We have a complex XML config file. Some people edit it via an XML editor, others edit it using a fill-in-form UI. With the fill-in-form UI, we'd like to display (and permit editing of) the XML comments, but it isn't always possible to work out programatically which XML element(s) the comment is talking about.
Compare:
<!-- The following setting is blah blah blah -->
<foo-setting>32</foo-setting>
<bar-setting>64</bar-setting>
with: <!-- Email Configuration Section -->
<mail-host>mail.example.com</mail-host>
<mail-port>25</mail-port>
In the first example, the comment belongs the immediate subsequent XML element only. In the second example, it belongs to both of the following elements. There is no reliable way to distinguish these cases, unless you try to interpret the natural language text of the comment itself (which can't be done with perfect reliability).>https://plus.google.com/+DouglasCrockfordEsq/posts/RK8qyGVaG...
The link there goes to G+ and thus a dead end. Anyone have that link?
I would have liked to see some alternatives / options that the writer likes.
> I removed comments from JSON because I saw people were using them to hold parsing directives, a practice which would have destroyed interoperability. I know that the lack of comments makes some people sad, but it shouldn't.
> Suppose you are using JSON to keep configuration files, which you would like to annotate. Go ahead and insert all the comments you like. Then pipe it through JSMin before handing it to your JSON parser.
HN discussion is here: https://news.ycombinator.com/item?id=3912149
I rave about JSON. I don't understand the rants with config files, but I could bet $20 that they _always_ begin with "but my comments!".
In the past I've promptly replied with (copied from my Reddit post):
Douglas Crockford said to use comments in json config files:
"Suppose you are using JSON to keep configuration files, which you would like to annotate. Go ahead and insert all the comments you like. Then pipe it through JSMin before handing it to your JSON parser."
Which was at the following link before Google killed Google+
https://plus.google.com/+DouglasCrockfordEsq/posts/RK8qyGVaG...
Here's my flag solution with JSON config files: https://github.com/zamicol/jsonflag I've never read this "arp242.net" article, but my solution anticipates some of its complaints, such as environmental variables, and addresses them. What I mean to get at is that this isn't rocket science and the "deficiencies" of JSON are flexibilities left to be easily addressed by the programmer. JSON is a great level of complex without being too much.
Every time this comes up, I don't understand how https://json5.org isn't superior to YAML in every way. If there was some deficiency with JSON5, just simply use JSON with comments. It's that simple.
JSON is one of the best things to ever come out of the CS disciplines. Moreover, JSON is a javascript subset. If json isn't sufficient, why not first try a different javascript subset/json superset before trying something more alien?
// When enabled, hovering over a word will show a popup listing all
// possible locations for the definition symbol. Requires index_files.
"show_definitions": true,
The author does highlight an important point though: if you're using JSON for your program's config and your program's parser doesn't support comments, you may want to find a parser that does.The author also mentions readability, which I think is subjective. I find JSON plenty readable provided key names are descriptive and the JSON is well-formatted. Readability does suffer when you have multiple nested objects. I'll also agree to the annoyances of forgetting a trailing comma somewhere.
Often, software offers commands to adjust config parameters (eg. PostgreSQL's ALTER SYSTEM command) or even a GUI interface for changing the configuration.
In these cases, if your config file format supports comments, or like the author suggests, even arbitrary code, then it's unlikely the software can automatically alter the config file. And even if it could, comments would quickly become outdated.
PostgreSQL addresses this issue by using multiple config files that can override each other, which can make debugging very hard, if you're not aware of the fact.
Issues like this can be avoided if you have a declarative config format like JSON, and the lack of comments actually becomes an advantage.
So in my opinion, if your config files can be changed by software, JSON is actually a very good option.
Config files which are expected to be written by both humans and machines are a fairly rare use case. Most config files are either editable by hand (e.g. /etc/), or editable though an UI (e.g. Firefox), but not both.
I find JSON to be much more readable than alternatives like YAML and much more flexible than plain key value pairs. I've been using YAML for a while with Kubernetes but I find it difficult to follow the indentation (especially if my IDE does not support guide lines)...
Strictness is a good thing for config files and makes it easier to find things.
Programmability in config files is generally not a good idea. Config files should be simple enough so that users with minimal technical ability can change the behavior of the system without having to know how to code.
If/when JSON5 becomes mainstream and IDEs stop showing warnings about comments in JSON, I will stop using .js files for my Node.js applications and just use .json. I do think that the commenting ability is important.
For the software folks -- why not adapt the tool or spin off a similar tool to handle these very common use patterns?
However if you’re building another nginx then obviously you’d want something more composable.
The problem that the article doesn’t address is that there isn’t a single good common standard. YAML has its own problems, TOML is fine for simple configs but can quickly get less readable when you start having deep nested elements. None of those allow conditional elements. So then you have HCL, which is an abomination and a bunch of other vendor specific formats - most of which don’t have reusable APIs. So do you then create your own format? And in which case your users have to learn yet another config language.
It’s unreadable in Brave on my phone.
I'll take a look later. Thanks for taking the effort of reporting it!
Version 1.6.6 (18.11.15.17)
I’m using an iPhone SE
@media (max-width: 26rem) {
html { font-size: 12px; line-height: 150%; }
}
a { transition: all .2s; }
I added that transition for a slightly prettier colour change on hover. What was going on is that this would also animate the size of the font, triggering some back-and-forth between the regular font size, and the smaller one in the media query.I fixed it by changing to:
a { transition: color .2s; }
I think this is how it's supposed to work. It's just the iPhone SE just to happens to be at the exact size to trigger this? I'm not sure, I wasn't really able to trigger it in desktop Chrome or Firefox.At any rate, thanks again :-)
Just a file with variable declaration that gets read into the runtime. It's definitly more readable for non tech users than json.
Configuration should be done with operating system packages and shell scripting; then there is no need for comments, since the files for making the package are kept in a revision control system like Bitkeeper or Mercurial and configuration on systems never modified by a human.
See a tag or section you don't use / parse? Copy that through to the output; or only over-write the parts of the structure that the parser understands... Something to help comments and other unsupported items in the config to the output file.
"XML" expressed configs have this problem too. Way too many projects that claim to process XML stuff will also just eat the config, remember what they understand as options, and throw away everything else, comments included.
EDIT: and if you want fixed size numbers (for many good reasons) then what size?
JSON as a config is another story, and I completely agree with this link. I'm a personal fan of TOML[1] for many purposes.
ini, yml, etc are nice but you'd need to import a parsing library and that may be slow/overkill for a small app. xml works with xmlhttprequest though.
I don't even like the term "best practice" nowadays. 9 times out of 10 it's an opinion on a best practice.
I prefer to just get shit done, do my best what's right, learn from great colleagues and peers on the job, and filter out the majority of this noise.
They only had "don't do this but since it's all we got, do it anyway".
Feel the luxury of choice not complain about it.
(though of course many people do both)
All the benefits of JSON with none of the negatives.
I agree overall YAML looks pretty nice, and consistent whitespace is a strong preference (I have pre-commit hooks), breaking user (if you have them) config if they don't know about this is an issue tho.
[1]: https://github.com/lightbend/config/blob/b782a2d701fc2b04579...
Who is interchanging information? In 99% of the cases, which is the target of the blog post, it's a human/developer and some software writing and reading a JSON file ON THE SAME MACHINE, IN THE SAME ENVIRONMENT. That is coding! You can do a far better job controlling a piece of software by actually writing code, telling it how to run.
Reversely, if you need to configure two pieces of software via the same config file, you need something that both can read, so you might go with JSON, XML, YAML, your flavour of the day.
But think for a second: does that happen more than 1% of the times? It doesn't!
Software like eslint (js), babel (js), emacs, etc are using configuration written in their appropriate languages, not in a serialized format. This allows for huge benefits.
If you had the chance to work with AWS for instance, you would have noticed how their Cloudformation templates are insane. They have pseudo functions, control statements IN JSON (and YAML FWIW)! On the other side of the globe, GCP teaches its customers to use python or jinja (a python template engine) to GENERATE YAML, though you can obviously use whatever language you want, which is then consumed by GCP.
Also, I don't think json is that bad for quick software utities, if you're already using it for data interchange, there are parsers for every language, I think it's a superior option to designing a custom config language (up to a point of course).
It's an effortless post just to rage against JSON. Why is this even on the front page of HN?
I've created the SANE configuration format [0] because I also found JSON (and YAML, and TOMl...) Faaaar from optimal.
It's kind of a JSON but designed for Human to Machine usecase (instead of machine to machine)
However, one could use Python as a configuration language! It could fill in the configuration object, which will then be transferred to the main app using JSON, YAML, XML, or something better than that.
You even have inline comments if you use an editor plugin like gitlens (VSC).
I guess the problem here though is portability
On that: https://youtu.be/0pX7-AG52BU
Plain text equals configuration setting is still a good idea.
Try to open a jupyter notebook with a text editor. What you expected to be a cleanly laid out python program with a few nicely formatted comments is in fact a nightmarish JSON "text" file, that looks like an obfuscated perl program.
But also:
JSON files are easily readable by default, in a lot of languages. Knowledge about (the limitations of) JSON can easily be assumed. Alternatives like YAML, TOML or any of the others all have their own idiosincracies.
And non-programmability is a good thing. It might be more verbose, but you don't have to fire up a javascript environment or bash interpreter or some such just to read a configuration file. And separate code paths are very explicit since that will mean separate objects or even files.
• Lack of comments - Config files are usually pretty self explanatory
• Readability - Never been an issue
• Strictness (complaints about trailing commas) - Who cares
• Lack of programmability - Does YAML offer this? I don't know YAML.
What are the alternatives?
Edit: After 35 minutes of comments here - looks like we have zero consensus and the article is trying to prescribe a preference.
Really? I have never seen a JSON config file that doesn't require reading external documentations. Comments are also useful when you leave a note on a specific configuration for your system.
In the universe where I live, most settings in a config file are pretty explanatory, but also, almost every config file has at least one setting that isn’t.
Also, even if I clearly understand the configuration file I’m reading, figuring out how I can change it often isn’t clear at all:
- If a json attribute contains a file path, can it be a URL or S3 object reference, too?
- does that path to a .gz file in an attribute mean input must be gzipped, or does the program look at the extension? If the latter, what compression methods are supported?
- If I don’t want to write that second output file, do I leave out the ‘extraOutput: foo.txt’ path setting, set it to null, set it to an empty string, or add an optional attribute ‘produceExtraOutput: false’ that defaults to ‘true’?
- what is the range of acceptable values for that ‘foo’ attribute that has a value of 42 in the config file I’m reading?
- is that ‘progressInterval’ value measured in bytes read, input lines, records processed, lines written, or wall time seconds?
- what other options that aren’t in this config file might I want to set in mine?
Yes, all of that could be in the documentation, but it’s easier for me if the config file contains comments describing that, and, in my experience, it also is easier for programmers writing the code to add such comments to the sample config file than to keep a documentation page in sync with the code and the sample config file.
Finally, some things cannot be in the documentation of the tool because they are local changes. For example, logging config could deviate from company standard for a reason.
Reading the two screeds together, though, I think the author has ignored one key idea of both JSON and YAML as formats: their use-cases are complementary, and the "problems" with each of these two formats only arises when you're using one for the use-case best suited to the other.
YAML's use-case is for human-written configuration. That's why it has so many weird ways to write things: to allow humans to write some data down in a way that works best to communicate said data to other humans, in a way that machines can coincidentally read. (See also: AppleScript.)
And that's also why YAML is "so powerful that it's basically inherently insecure." You shouldn't be executing untrusted YAML any more than you should be executing an untrusted bash script. YAML is a language for the deploying sysadmins of a piece of software to configure said software with; it's not a language for the tenants of a system to communicate their needs to the system with.
JSON's use-case is for building machine data generators and parsers in a distributed system (like the web) where it is very important that the data be easily introspected by humans, and that humans will also be able to modify or otherwise "poke at" said data, in flight, using simple text-based tools. But the intention isn't that humans will ever author JSON "at scale." JSON is like HTTP/1.0: it's something that a human can write, in a netcat session to ensure things are working or to probe at a system's capabilities or responses; but where a human writing in said format is never going to be the "proper" way of doing things in production.
Now consider the subheadings of each screed:
"JSON as configuration files: please don’t"
- Lack of comments
- Readability
- [Too Much] Strictness
Obviously, if these are your problems, then you're a human (probably a sysadmin) hand-rolling configuration. Use YAML.
"YAML: probably not so great after all"
- Insecure by default
- Can be hard to edit, especially for large files
- It’s pretty complex
- Surprising behaviour
- It’s not portable
None of these matter if you're a human sysadmin who is interactively hand-rolling a configuration file for a piece of software. YAML "doesn't scale", but configuration files aren't a domain that requires thousands of lines or huge, deep hierarchies. And the complexity, surprises, and portability don't matter if you're not trying to "shout YAML into the void" without knowledge of what the target system is.
If those are your use-cases—if you need scalable, portable, secure, predictable data—then you're definitely not writing a configuration file, but rather are just interchanging data of some kind. Use JSON.
---
Now, the funny thing to me is, JSON is a subset of YAML.
(And that's why https://leebriggs.co.uk/blog/2019/02/07/why-are-we-templatin... was written: nobody should be templating YAML when machines are perfectly capable of emitting JSON that YAML parsers will happily accept.)
Really, rather than talking about which format to use, what this discussion should be about—in a better world than the one we're in—is the use of "trusted mode" flags in YAML parser configuration.
Ideally, you should be able to take a YAML parser, and tell it, per input stream, that "this is the hand-rolled sysadmin kind of input, so parse it using the full feature-set of YAML"; or "this is the J. Rando user kind of input, so parse it like a JSON parser with maybe one or two more features" (ala StrictYAML.)
That would solve most of the complaints the author has, on both sides: if config systems used YAML parsers but only configured them to accept a strict subset of said YAML (either StrictYAML or plain JSON) for any input they expected to come from anywhere other than a sysadmin, then there wouldn't even be a discussion here. You'd use a plain JSON parser for (efficiently, securely) receiving plain data; but for configuration, a YAML parser would be the obvious choice, because it could always be locked down.
But in the world we live in, YAML parsers don't have security features like this.
I'd make a suggestion of an alternative, which in pseudocode looks like this:
opt.parse('-c', '--config [path]', Path) do |path|
this.config = JSON.parse(path)
end
opt.parse('-C', '--config-trusted [path]', Path) do |path|
this.config = YAML.parse(path)
endI'm the author of the "yaml" npm package, and would be interested to hear more precisely what security features you'd wish for a YAML parser to have. In other words, if there's one I've missed, I'd like to fix that.
* Config should never, ever be written by the application which depends on it. Ever. I don't care what you think your rationale is: no application should modify its own configuration.
- Git is a weird case which springs to mind, but technically `git config` is separate from eg. `git fetch`. So no real problem there.
- .NET's bidirectional config bindings are daft. If you stick to reading only then the framework's fine, but otherwise it rapidly heads into crazytown.
- If you think you need to violate this rule, probably your 'config' is actually user data. If so it should be treated as such, ie. it's subject to data migrations, the admin doesn't need to touch it during upgrades, etc.
* Config needs to be strict. It must be possible to statically verify the basics, and there should not be any confusion over meaning in eg. some random diff.
- Everything's a string in a text file. The way in which the string is interpreted must be absolutely unambiguous and sane.
- YAML need never apply.
* Config must not be Turing-complete. Ever. If your configuration can be every conceivable output of a program then you have built a system which is effectively impossible to properly test.
- I take this one step further and consider it idiotic to include code in a config file at all. It's fine to generate one from other files as part of a deployment, but the config read by the application fucking better be a single static text document which maps to a simple data structure, with as little processing as possible performed after the fact.
* Config needs to be possible to process in a totally reliable way by a deployment process, eg. templating.
- See above.
* Config should succinctly describe concepts which make sense for configuring the application, not the dozen or so libraries upon which it depends.
- In .NET terms, this means "don't give us ten megs of 'default' WCF crap to fiddle with". Give us an endpoint address, and sufficient documentation/logs/etc to figure out why 'http://something' doesn't work for an app written only for 'net.tcp://something'.
- Logging is a weird one. I'd generally handle this by providing app-level levers for expected logging requirements, then explicitly calling out the framework and version used and where to find the docs for the advanced stuff.
Given the above, I fail to see anything at all wrong with XML. It has strong support for schema validation and templating, and can handle any complex data structure it needs to in both contexts.
Saving keypresses is not a valid excuse. Terse crap is still crap.#
While longwinded pointless junk has given XML a bad name, particularly in the Java and .NET world, being explicit about things retains stability in the face of supposedly-unrelated changes: if your configuration's meaning can change globally if a single rule or heuristic is modified then your software's reliability is in question.
All it costs is keypresses, and text diffs are very easy to audit in source control.
Write compact config in anything you must, generate the big stuff, commit it, record it, audit it. And have CI check that the compact matches the big.
Disclaimer: ten years ago I hated XML for wasting so much space. I was an idiot. These days I'll take abundant permanent precision over everything.