5 ms·
I feel like nobody ever ends up having this discussion about JSON. Generating JSON data using string interpolation or templating is clearly wildly insane, righ
by jameshart 3y ago
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’
- pphysch 3y agoI'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.
- jameshart 3y agoA 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 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)
- jlokier 3y ago> Generating JSON data using string interpolation or templating is clearly wildly insane, right? You don’t do it. I'm sorry to report that I've seen a lot of JSON generated by string concatenation and templating, in different projects. Often using 'printf' or 'echo' in various languages. Sometimes using whatever's used for HTML string templating if the JSON is embedded in HTML or served as a resource similar to HTML. Yes, its horrible and breaks if fed variable values that have characters like quotation marks in. People do it anyway. Even in languages that have perfectly good data structures and JSON libraries. I've seen a fair amount of parsing values out of JSON using regexes too, assuming specific formatting of the supplied JSON.
- jameshart 3y agoBut unlike with HTML I don’t think anyone’s going to jump in here and say ‘well yeah! Of course people do that because JSON is just text!’ I think everyone agrees that those approaches with JSON are bad.
- a1369209993 3y agoWell yeah! Of course people do that because JSON is just text! It's less common, because JSON is simpler, so the tradeoff point for using a grammar is lower, but it still makes sense in things like shell scripts, and other cases where the equivalent of `print(obj)` (or `eval(totally_not_rce)`, but let's pretend that's not available anyway) doesn't happen to produce (or consume) valid JSON by coincidence. # Using: grep -oP '(?<="bar": ")[^"]+' foo.json # and printf("{\"count\": %i, \"type\": \"%s\"}\n",nfoo,tfoo); is a general-purpose solution that can be adapted to pretty much any text-based format just by looking at examples, without having to cross-reference with a external specification (that the thing you're feeding input to or pulling output from may not even correctly implement anyway), so obviously people do that! See also various discussions under the heading "Worse is Better". Whether it's the right thing or the wrong thing, it very clearly is a thing.
- jameshart 3y agoOh.
- verdverm 3y agoI guess you have never authored a Helm template, which does on worse and templates Yaml...
- hayleox 3y ago"Nobody ever generates JSON using string interpolation." Oh boy, you just took me back to my first ever PHP project. Feast your eyes: https://github.com/oxguy3/phpStageManager/blob/master/eventsfeed.php https://github.com/oxguy3/phpStageManager/blob/master/events... This file generates a feed of events (rehearsals for my high school play) to be rendered by the FullCalendar JS plugin. FullCalendar required a particular data schema that didn't match the format of my MySQL table, which meant I couldn't just json_encode() the MySQL results. I guess I just didn't conceptualize that I could create a new object that matched the FullCalendar format, and then call json_encode(). So, I generated JSON with strings. Honestly it's a toss-up whether the JSON generation is the worst thing about this file. It looks like I also made a separate database query for every single row to get the username, because I apparently didn't know how to do joins. Could probably spend an hour listing some of the other little nuggets of awful in there. But hey, it got the job done! :)
- hirvi74 3y agoImperfect is better than incomplete. All is fair in love, war, and programming.
- 1123581321 3y agoAwesome. Yes, put this out there! We've all started somewhere or just had to get something shoveled out.