Practical JSON at the command line using Jello (Part 2) #29
kellyjonbrazil
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In Part 1 I went over some ways to sort a JSON array of objects to get the data you are looking for. In this post I'll use the same method to demonstrate how to effectively assign the JSON results to various types of BASH variables and use them in BASH scripts.
Assigning a single value to a BASH variable
In the last post I described this
jello
query that outputs the newest MIT-licensed package installed on the system:Assigning this value to a BASH variable is fairly trivial:
If you blinked, you might have missed it, but I added the
-r
option tojello
. This is the "raw" option which strips strings of their quotation marks. This option is typically used when assigning values to BASH variables, since the quotation marks are often not desired.That was easy... now let's go to the next step.
Assigning a list from a JSON array
Now, instead of assigning a single value, we'll assign a word list to a BASH variable:
I have simplified the query here a bit by only outputting the name of each package that is MIT-licensed. Also notice I added the
-l
jello
option. The-l
"lines" option converts the JSON array output into a word list that can be assigned to a BASH variable. Typically the-l
and-r
options are used together so strings have their quotation marks stripped as well.The output of this script looks like this:
That's pretty useful, but let's go to the next level by assigning an entire JSON array of Objects to a BASH Array variable.
Assigning a Bash Array from a JSON Array of Objects
Sometimes you might want to assign an entire JSON Object to a BASH Array element so you can pull multiple attributes out and use them as variables. For example, if we want to print the package name, version, and vendor we could do multiple for loops (one for each variable) and then print them, but that would be terribly inefficient.
By assigning to a BASH array, we can loop through the rpm data one time, and then just pull the attributes we want while we loop through the results once:
As you can see, we are using the
while read -r
BASH idiom to assign each JSON Object output byjello
to its own BASH Array element.mapfile -t packages < <( ... )
can be substituted for thewhile
loop when using Bash 4.0 and higher.The
-l
, or "lines", option in this case changes the JSON Array into JSON Lines, which can be ingested by BASH. We don't need the-r
, "raw", option this time because we are not outputting string values, but entire JSON objects.Then we loop through the BASH array and pull the JSON attributes we want just-in-time.
And here is the output:
There you go - hope that was helpful. For more details, see the original article where I used
jq
instead ofjello
.Beta Was this translation helpful? Give feedback.
All reactions