JSON Fortune
4th of December 2026
10 min read
1871 words
TLDR
I made a bash script that allows users to run a fortune like app, with a custom database built on JSON, as opposed to the .dat file generated with strfile that vanilla fortune expects when piping in a custom path. The script can be downloaded from both Codeberg and GitHub.
Install Instructions
- Install dependencies:
| Distribution | Install Command |
|---|---|
| Arch Linux | sudo pacman -Syu git make jq |
| Fedora | sudo dnf install git make jq |
| Ubuntu | sudo apt update; sudo apt install git make jq |
- Clone a repository:
git clone https://codeberg.org/makccr/fortune-json
# or
git clone https://github.com/makccr/fortune-json
- Build from source:
cd fortune-json &&
makepkg -si
- Optionally download and use my JSON quotes file:
wget https://makc.co/_data/quotes.json &&
fortune-json ~/Downloads/quotes.json
How Did We Get Here?
Several years ago now (seven years, based on the YouTube video I made on the subject)[1], I discovered the fortune app. This was all the way back in the days before I swapped over to running Linux full time, and I simply happened upon an app that could display randomly generated quotes. It wasn't long before I figured out what a .bash_profile was, and had a random quote generated every time I opened a terminal window, and only slightly longer before I figured out how to use strfile to create a custom database of my own quotes and pipe that into fortune. That discovery led me down the path of Linux ricing, and planted the seed that turned into one of my favorite hobbies. Though it may sound a bit absurd, it also was a not insignificant stepping stone on the path towards me swapping off of macOS and onto Linux, getting into tiling window managers, and much more. Most importantly, however, for the last seven years I've consistently had a nice little piece of wisdom or sarcasm to greet me, every time I spawn a new shell.
Cut to a few weeks ago: I decided that all the users of my personal website needed to have the same blissful experience, on every single page of my website. I spent some time putting together a quotes database[2], by converting my existing fortune database to a JSON file that I could reference through my static site generator, 11ty. I then set up the world's dumbest Async function to randomly select and display a quote on the bottom of every single page on my site:
<script>
(async () => {
try {
const response = await fetch("/_data/quotes.json");
const quotes = await response.json();
const random = quotes[Math.floor(Math.random() * quotes.length)];
document.getElementById("quote-text").textContent = random.quote;
document.getElementById("quote-author").textContent = "" + random.author;
} catch (err) {
document.getElementById("quote-text").textContent = "Stay curious.";
document.getElementById("quote-author").textContent = "";
}
})();
</script>
Unfortunately, I soon realized that I had broken one of my cardinal rules of life. I'm a fundamentally lazy person by nature. I'm perfectly willing to do hard work when the time to do work presents itself, but I simply cannot abide inefficiency in any process that I use. Even something as small and insignificant as having to maintain two different copies of my quotes database: one for my website and one for my offline terminal use, is enough to drive me absolutely crazy if I force myself to live with it for long enough. The situation had to be remedied.
My first idea was to simply create a bash script to solve the problem for me, as this is my general approach to making my life easier. My ~/.scripts/ directory is full to the brim with miscellaneous scripts that solve one problem in a use case so specific that I have to be the only person in the world who gets any use from it. So I set out to simply create a script that would convert my new JSON database to something that I could run through strfile and use to generate a new fortune database - or vice versa. I wasn't super picky about which of the two databases I would use as the master in the relationship, but I knew that the plan was to simply edit one, and have both files end up changed. As luck would have it, it was pretty easy to create a simple Python script that could remove all the JSON formatting, creating a simple text file that could be converted to a .dat database with strfile. I also couldn't help but acknowledge that it would be very easy to set up a custom macro in Vim that could do the same thing, but with less initial set-up compared to a python script. But, as is so often the case, I soon realized that what I had constructed in my head was not the simple solution I had dreamed up, but a multi-step process that required me actually remembering to trigger it anytime I changed one of the databases:
- Change a quotes database, and trigger a script to change the next one.
- Convert
JSONto plain text, or plain text toJSON. - Generate a database file with
strfile.- Remove existing database, and clean up any temp file.
While I certainly could have just set up a few smaller bash scripts, a python script and a master script to run everything, maybe even set up a new crontab entry to watch for changes in the directory of one of the two databases, I simply decided that the best thing to do would be to find a way to use one database for both applications. Since the json setup was so easy with my static site generator of choice, and I was unlikely to achieve the same level of customization potential using a plain text file for my database, it became clear to me that what I needed to do was modify the fortune program to accept input in json.
The Script
After longer than I care to admit, I realized that there was a preexisting JSON processor available, called jq[3] that would enable precisely the thing that I needed. Once again, after more time than I care to admit, I was able to piece together a script that would do the job in one go. But eventually I managed to create something that was not simply a supplemental add-on to the fortune app, but rather a script that runs on its own and does precisely the same thing that fortune does, just using a JSON file as input. Using jq, I was able to create a script that:
- Reads the JSON file directly via
jq. - Selects a random quote from the JSON array.
- And then extracts a quote and author from the array and prints them to the terminal in the same fashion that fortune does.
As a disclaimer, with any code that I put out on the internet, no matter how simple it is, I always like to acknowledge that I am not a developer by trade and have, at best, a limited understanding of most of the tools that I use to create the things that I want. There is some basic error trapping in the script, but as is always the case, any user with more knowledge than me is encouraged to submit pull requests to improve the script on Codeberg or GitHub.
Expected Formatting
One of the chief advantages for me in creating this little script was the implementation of far better formatting via JSON arrays than was previously available to me using the vanilla fortune app. My previous workflow consisted of creating a plain text file with the quotes and a delimiter character on a new line, and then running it through strfile and manually creating the database file that fortune expected. Whatever formatting options might have been available to me through this workflow certainly weren't being utilized, but they are now with the new script. As a result though, the new script expects a JSON array formulated in a specific way:
[
{
"quote": "Matter. How tiny your share of it.\nTime. How brief and fleeting your allotment of it.\nFate. How small a role you play in it.",
"author": "Marcus Aurelius"
},
{
"quote": "Give yourself to a few and to those few give heavily.",
"author": "Rupi Kaur"
}
]
As the sharp-eyed among you might have noticed, I'm also now able to utilize \n to create a newline in the quotes, and use all of the formatting options that come with JSON. It's also worth noting that this specific array formatting can be modded quite easily by anyone with the interest to do so. But at least for now, I have a solution to my very first-world problem, and if anyone with the same issue happens upon this essay, they now have one as well.
Footnotes
MAKC, "How to Create a Custom Fortune Database", YouTube, 17th of September 2019.
For anyone interested, this video walks through the process of creating a
.datdatabase based on a text file, whichfortuneexpects by default. ↩︎MAKC, "Quotes Database for MAKC.CO", makc.co, 2026. ↩︎
Morgan Adamiec, Leonidas Spyropoulos & Felix Yan, "jq", jqlang.org, 2026. ↩︎

