Formatting JSON for readability
Charly's Column – JSON
Armed with just json.tool and jq, Charly preps the JSON data delivered by his Philips Hue bridge so that even humans can read it – an essential step towards improving the usability of his home automation system.
Some time ago [1], I briefly talked about how I use the measured values from a lux sensor to control a Hue lighting system. In redecorating my hallway, I added some new lights and removed others. I couldn't remember the light sources' IDs, so I asked the Hue bridge to dump the configuration. What I got was hard-core JSON, but unfortunately not in a human-readable format (Figure 1).
There are plenty of tools to make JSON readable. I started with what I already had in place, json.tool
, which the Python installation had dumped on my hard disk. I wrote its neatly formatted output to a file in /tmp
for further processing (Listing 1).
Listing 1
JSON Query
$ curl --request GET 10.0.0.10/api/w25-4kqL7d|python -m json.tool > /tmp/hue.all
Listing 2
Light Source Script
01 #! /bin/bash 02 WDIR=/usr/local/shellscripts/lux 03 TMPDIR=/tmp 04 HUEBRIDGE=10.0.0.10 05 USER=w25-4kqL7d 06 07 for i in $(seq 1 20); do 08 echo "trying light $i"; 09 LAMPTEST=$(jq -r -M ".lights.\"$i\".name" /$TMPDIR/hue.all); 10 if [ "$LAMPTEST" != "null" ]; then 11 echo "Light $i exists: $LAMPTEST" 12 fi 13 done
However, I didn't need the data in Python; I wanted to use the output in a small Bash script. This prompted me to continue processing with jq
[2]. The tool claims to be a kind of sed
, awk
, and grep
for JSON. I wanted to find out the current IDs for my light sources, as well as discover their plain text names. Thanks to jq
, a tiny script (Listing 2) is all it takes to do this. When I ran it, it gave me the output shown in Listing 3.
Listing 3
Script Output
Light 4 exists: LR_ceiling_desk Light 5 exists: LR_ceiling_sofa Light 6 exists: ColorWall1 Light 7 exists: Hall_1F Light 8 exists: Hall_2F Light 9 exists: Hall_3F Light 10 exists: LRTinkeringCorner
The reason why IDs 1
to 3
do not exist is because I dismantled the lights and removed them from the Hue app. The Hue bridge has no reason to re-sort the remaining IDs – which is fortunate, because that would cause total chaos every time I changed a lamp. As jq
once again underlines: The shorter a tool's name is; the more important and powerful it is. Lights on!
Infos
- "Charly's Column – Hue and Rasp Pi" by Charly Kühnast, Linux Magazine, issue 218, January 2019, p.39, https://www.linux-magazine.com/Issues/2019/218/Hue-and-Rasp-Pi/(language)/eng-US
- jq: https://stedolan.github.io/jq/
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.