![]() ![]() The DSL for filtering, querying, and creating JSON goes much deeper than what I’ve covered here, so see for the full documentation. Jq is awesome and makes working with JSON in bash easy. ![]() We have a huge front-end monolith with a single package.json that has 250 dependencies ?, so some automated assistance was necessary. I used something similar to this recently at work to prune unused dependencies. There’s more that could be done to the grep-ing in that script to make it more robust, but that’s the basic gist. We tell it to start a bash subshell where our grep_dep function is called with it’s args P 4 defines the concurrency, so 4 concurrent greps ep () Finds the elements of an array which satisfy a filter function. Let’s say we have JSON that looks like this: defines the replacement string where the dependency string will get placed Luckily, it’s really intuitive (unlike awk ?). Also like sed or awk, it basically has it’s own domain specific language (DSL) for querying JSON. Jq works similarly to sed or awk - like a filter that you pipe to and extract values from. See jq’s install help page for how to install on other environments. Jq isn’t a built-in command in any environment, so you have to install it. To me, bash is more expressive and succinct for certain tasks than node is. For most automation tasks, I like to use bash whenever possible because it’s faster and even more portable (I can share a bash script with team members that don’t have node.js installed). Why not just use node.js when you need to deal with JSON? Other objects are iterated via their named properties. Arrays and array-like objects with a length property (such as a function’s arguments object) are iterated by numeric index, from 0 to length-1. The jQuery grep () function removes the items from the array which do not pass the filter function or condition test, keeps only items that passed the filter function. A generic iterator function, which can be used to seamlessly iterate over both objects and arrays. The jQuery grep () function is a built-in function in jQuery. By making JSON easy to work with in bash, jq opens up a lot of automation possibilities that otherwise required me to write something in node.js (which isn’t bad, it just takes longer generally). The jQuery grep () function is used to finds an element of an array that satisfies the filter function. Jq can simplify the above bash to this: curl -s "" | jq '.value.joke' Luckily there’s a better way using a tool called jq. You have to pipe to 4 different utilities just to get to a property in the JSON response body! Bash doesn’t understand JSON out of the box, and using the typical text manipulation tools like grep, sed, or awk, gets difficult. That’s tough to read and even tougher to write. (Note: the above code was taken from, which is a great article). As weve seen, these are actually methods of the jQuery object, but practically. Perhaps you’ve seen or even written bash that looks like this: curl -s '' \ ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |