Merging and Sorting Arrays with jq

Every once in a while I need to add more mod information to the TA Demo Archive application. I built a tool a year ago to make this process easier, but it assumes that there’s only one archive storing all of the mod data. The updates now contain multiple files, and I end up having to use my tool on each file. Then I have to merge all of the extracted data before loading it back into the application.

Assuming you have a few .json files that are arrays of the same type of thing, you can run this pipeline to merge and sort them. The -s option stands for slurp. This reads the entire input stream into an array and runs the filter once.

cat f1.json f2.json f3.json | jq -s 'add | sort_by(.field_to_sort_by)'

Now might be a good time to upgrade the extractor tool to handle multiple files!