I had some modifications to make in some very large JSON files in multiple locations that have no formatting at all. I’m pretty good with vi, but I’d rather not do that more than once. So I did what any lazy engineer would do: I scripted it. And I learned some things.
First I knew that I had a general idea of the shape of the script I wanted so I asked ChatGPT to get me started with a prompt:
“write a bash script that accepts a single filename argument, reads its contents into a file, makes a minor change to them, and then writes them out to another file name”
That produced the following:
Getting the “if I didn’t get my argument” out of the way along with reading the contents was nice.
I got down to the bit about
new_text and that was interesting.
I recognized it as a parameter expansion that looked similar to one I’d learned
when reading up on zsh. I tried to do my json replacement using that but there
were just too many braces to try and escape and I wound up giving up and using
jq which was my original plan.
jq looked like this:
jq '.app_filter["some_guid"]["some date"]["key"] = ["foo","bar"]'
That replaced the value with the new foo/bar value that I wanted. I did some
rg to make sure that the change was
just what I wanted.
graphtage ./old.json ./new.json 2>/dev/null > diff.json rg interesting_bit -C 10 ./diff.json if jq -e . >/dev/null 2>&1 < $filename; then echo "Parsed JSON successfully and got something other than false/null." else echo "Failed to parse JSON, or got false/null." fi
Links for those utilities are here: