Because people are taught to think in strings. And programming languages coddle them with tools like concatenation and string formatting. And because we let people think they can do useful things with strings as a result.

But what people actually need are grammars.

The exact same reason why parsing HTML with a regex unleashes Zalgo is why generating HTML with string templates is bad. Because both treat HTML as a string, not a grammatically restricted language.

> [P]eople are taught to think in strings[, b]ut what people actually need are grammars.

I don’t actually disagree with you for the most part, but I feel that an important caveat has gone unacknowledged.

Grammar formalisms have the same weakness compared to dealing with raw strings as sound static type systems do compared with dynamic typing: there are small, mostly isolated islands of feasibility in a sea of intractable (often undecidable) generality, and if your problem doesn’t fit inside those borders things start to get nasty (cf how even GCC’s handwritten rec-descent parser didn’t get its lexer hack interactions correct in all cases[1]).

I still agree that we spend criminally little time on syntax. Starting with the simplest cases: with how much time is spent in school on “order of operations” you’d think we could take a moment to draw[2] a damn syntax tree! But nooo. There are in fact working mathematicians who don’t know what that is. (On the other hand, there are mathematicians who can explain that, in a sense, the core of Gödel’s incompleteness is not being able to reason about arithmetic—it’s being able to reason about CONS[3], which arithmetic happens to be able to very awkwardly do.)

[1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67784

[2] https://mlochbaum.github.io/BQN/tutorial/expression.html

[3] https://dx.doi.org/10.1215/00294527-2008-028

I feel like nobody ever ends up having this discussion about JSON.

Generating JSON data using string interpolation or templating is clearly wildly insane, right? You don’t do it.

Maybe for some config file generation scenarios you might just run a template JSON file through a token substitution or env var interpolation or something. But you’d feel bad about it, because it’s so easy to NOT do it that way. And even then you’re not interpolating in JSON fragments like ‘“age”: 25’ - you’d have the decency to only interpolate in values like ‘25’.

In the node ecosystem it’s so easy to switch from a .json file to a .js file, too, if you want to build the json dynamically.

For some reason people feel more willing to attempt it with YAML. And then regret it when they realize how significant indenting has screwed them.

And then with HTML people just give up and go ‘yup, it’s all text, even the angle brackets’

I've used AWK to output (flat!) JSON in ETL pipelines, because it's blazingly fast and "zero" deps.

But I'd never try to implement my own parser or output deeply nested JSON.

A useful tool for transforming JSON to and from a format that is more amenable to simple text tooling is gron: https://github.com/tomnomnom/gron

Takes all the tree and hierarchy management away, makes it so ordering doesn’t matter.

If I’m generating JSON from batch scripts it’s my preferred tool (easier than fighting jq for many tasks)