JSON is a text-based data format that is widely used to store or exchange data between systems. The jq is a command line based JSON processor that allows to transform, filter, slice, map, or perform other operations on JSON data.
This tutorial shows how to install jq on Raspberry Pi.
Install jq
Connect to Raspberry Pi via SSH. Update the package lists and install jq using the following commands:
sudo apt update
sudo apt install -y jq
Once installed, check jq version:
jq --version
Testing jq
Now we can test. Let's say we have JSON data stored in the test.json
file:
echo '{"status":"success","data":[{"name":"John","age":25},{"name":"James","age":29}]}' > test.json
The jq tool supports various filters that can be applied on JSON data. For example, the dot .
filter prints unchanged but nicely formatted JSON.
jq '.' test.json
Command will output:
{
"status": "success",
"data": [
{
"name": "John",
"age": 25
},
{
"name": "James",
"age": 29
}
]
}
We can get a particular field of JSON object or an element of JSON array:
jq '.data[1].name' test.json
Output:
"James"
Uninstall jq
If you want to completely remove jq and related dependencies that is not used by other packages, execute the following command:
sudo apt purge --autoremove -y jq
The 2 Comments Found
Perfect!
I used this with Tasmota devices.
curl -s http://192.168.2.51/cm?cmnd=status%208 | jq -r .StatusSNS.ENERGY.Power
Thank you,
json returned from various APIs is easy parse on my Pis now. Thanks!
Leave a Comment
Cancel reply