Here’s a very quick little thing I just had to do, that maybe useful to you at some point in the future. The idea is that I had a JSON file (technically some JS to begin with), and I wanted to create a new file in the current directory for each entry in the JSON.
Here’s what I had:
import { Sector } from "../../types";
export const sectors: Sector[] = [
{
title: "Accounting",
slug: "accounting",
},
{
title: "Automotive",
slug: "automotive",
},
{
title: "Agriculture",
slug: "agriculture",
},
// ... etc
];
Code language: TypeScript (typescript)
That being TypeScript, I couldn’t directly take that as JSON because the title
and slug
keys are not quoted.
I guess I could have dumped this out with JSON.stringify
, but it’s easy to copy / paste that stuff directly into an online conversion website that already exists:
Copy and paste that ‘result’ data into a new JSON file.
For this I made a new temporary directory:
cd /tmp
mkdir /tmp/whatever
cd whatever
touch data.json
vim data.json
Code language: Shell Session (shell)
That fires up vim, but use whatever editor you feel comfortable with. I am no vim ninja, but I know enough for this kind of thing.
Once in vim, press i to switch to insert
mode, then right click and paste:
Once done, hit esc to exit insert
mode, and then directly type :wq
to write and quit (or save and exit).
You should be dumped back to your command line. Jolly good.
Now you need a new shell script.
I’m going to repeat the vim process.
touch a.sh
vim a.sh
Code language: Shell Session (shell)
Call your script whatever you like, I’m just lazy so a.sh
it is.
You should now be back in vim, so it’s time for more insert / escape / write and quit:
for item in $(cat data.json | jq -r '.[] | @base64'); do
_jq() {
echo ${item} | base64 --decode | jq -r ${1}
}
touch "$(_jq '.slug').ts"
done
Code language: Bash (bash)
This command will read the data from the data.json
file and iterate over each object in the array.
For each object, it will extract the slug
value using the jq
tool and create a new file with the name {slug}.ts
using the touch
command.
You will need to have the jq
tool installed on your system for this command to work. If you don’t, that is as easy as:
sudo apt-get install jq
Code language: Shell Session (shell)
In order to execute the shell script, it needs to be made executable:
sudo chmod +x a.sh
Code language: Shell Session (shell)
And then you can run it:
./a.sh
Code language: Shell Session (shell)
Which should give you:
Neat.