Save the file, then run the following command to make it executable:
chmod u+x ./script.sh
Check out the new feed reader by listing the latest issue in your favorite Github repo:
./script.sh 0
In addition to reading data from the API, you can also use jq to manage JSON files in your local machine. Start by creating a simple JSON database file using your favorite text editor:
nano ./database.json
Paste the following block of data into the file, then save it:
[
{"id": 1, "name": "Ramces", "balance": 20},
{"id": 2, "name": "Alice", "balance": 30},
{"id": 3, "name": "Bob", "balance": 10},
{"id": 4, "name": "Charlie", "balance": 20},
{"id": 5, "name": "Maria", "balance": 50}
]
Check if jq reads the JSON file correctly by printing the first object in the database array:
jq '.[0]' database.json
Execute a query on the JSON database using the 'Object Identifier-Index' filter. In this case, the author is looking for the value of the key '.name' on every entry in the database:
jq '.[] | .name' database.json
You can also use some of jq's built-in functions to filter queries based on certain characteristics. For example, it is possible to search and print all JSON objects with a '.name' value of more than 6 characters:
jq '.[] | select((.name|length)>6)' database.json
Additionally, jq can operate on a JSON database similar to a basic spreadsheet. For example, the following command prints the sum of the '.balance' key for every object in the database:
jq '[.[] | .balance] | add' database.json
You can even extend this by adding conditional statements to your query. The following will only add '.balance' if the second object's '.name' value is 'Alice':
jq 'if .[1].name == "Alice" then [ .[] | .balance ] | add else "Second name is not Alice" end' database.json
Variables can be temporarily removed from a JSON database. This can be useful if you are testing a filter and want to ensure that the filter can still process the data set:
jq 'del(.[1].name) | .[]' database.json
You can also insert new variables into the database using the '+' operator. For example, the following line adds the variable 'active: true' to the first object in the database:
jq '.[0] + {active: true}' database.json
Note: You can make the changes permanent by converting the output of the jq command to the original database file:
jq '.[0] + {active: true}' database.json > database.json
Another great feature of jq is that it can accept and work with non-JSON data. To achieve that, the program uses an alternate 'slurp mode, in which it converts any space- and newline-separated data into a JSON array.
You can enable this feature by passing data into jq using the -s flag:
echo '1 2' | jq -s .
One advantage of converting raw data to an array is that you can process them using array index numbers. The following command adds two values by reference to their converted array location:
echo '1 2' | jq -s '.[0] + .[1]'
You can develop this array position further and build new JSON code around it. For example, this code converts the text from the echo command into a JSON object through a curly braces filter:
echo '6 "Mallory" 10' | jq -s '{"id": .[0], "name": .[1], "balance": .[2]}'
In addition to getting raw data, jq can also return non-JSON data as output. This is useful if you are using jq as part of a larger shell script and only need the results from its filters.
To do that, run jq followed by the -r flag. For example, the following command reads all names from the database file and returns it as plain text data:
jq -r '.[] | .name' database.json