This post is older than a year. Consider some information might not be accurate anymore.
Used: gron v0.6.0
Do you ever need to find specific information in a large JSON data output? Most of us end up using grep
in the Linux console. There is another way to read structured JSON data. The solution is gron by Tom Hudson, and its goal is to make JSON greppable.
Installation
This section describes the installation for Mac OS with brew
.
Set permission for your installation
sudo chown -R $(whoami) /usr/local/sbin
Install it.
brew install gron
Usage
Most of the time in operations I check docker containers regarding their network settings.
An example:
docker inspect nginx | gron | fgrep "NetworkSettings"
The output:
json[0].NetworkSettings = {};
json[0].NetworkSettings.Bridge = "";
json[0].NetworkSettings.EndpointID = "403b4f8273f3d9e3f0cf15d66a3829bfdb64b2d6fda4f83417cf5831998c1292";
json[0].NetworkSettings.Gateway = "172.17.0.1";
json[0].NetworkSettings.GlobalIPv6Address = "";
json[0].NetworkSettings.GlobalIPv6PrefixLen = 0;
json[0].NetworkSettings.HairpinMode = false;
json[0].NetworkSettings.IPAddress = "172.17.0.2";
json[0].NetworkSettings.IPPrefixLen = 16;
json[0].NetworkSettings.IPv6Gateway = "";
json[0].NetworkSettings.LinkLocalIPv6Address = "";
json[0].NetworkSettings.LinkLocalIPv6PrefixLen = 0;
json[0].NetworkSettings.MacAddress = "02:42:ac:11:00:02";
json[0].NetworkSettings.Networks = {};
json[0].NetworkSettings.Networks.bridge = {};
json[0].NetworkSettings.Networks.bridge.Aliases = null;
json[0].NetworkSettings.Networks.bridge.DriverOpts = null;
json[0].NetworkSettings.Networks.bridge.EndpointID = "403b4f8273f3d9e3f0cf15d66a3829bfdb64b2d6fda4f83417cf5831998c1292";
json[0].NetworkSettings.Networks.bridge.Gateway = "172.17.0.1";
json[0].NetworkSettings.Networks.bridge.GlobalIPv6Address = "";
json[0].NetworkSettings.Networks.bridge.GlobalIPv6PrefixLen = 0;
json[0].NetworkSettings.Networks.bridge.IPAMConfig = null;
json[0].NetworkSettings.Networks.bridge.IPAddress = "172.17.0.2";
json[0].NetworkSettings.Networks.bridge.IPPrefixLen = 16;
json[0].NetworkSettings.Networks.bridge.IPv6Gateway = "";
json[0].NetworkSettings.Networks.bridge.Links = null;
json[0].NetworkSettings.Networks.bridge.MacAddress = "02:42:ac:11:00:02";
json[0].NetworkSettings.Networks.bridge.NetworkID = "00a571161e8b1042db9eef3a461797f3627cb7ce6e5d573ae546d7123bcdf946";
json[0].NetworkSettings.Ports = {};
json[0].NetworkSettings.Ports["80/tcp"] = [];
json[0].NetworkSettings.Ports["80/tcp"][0] = {};
json[0].NetworkSettings.Ports["80/tcp"][0].HostIp = "0.0.0.0";
json[0].NetworkSettings.Ports["80/tcp"][0].HostPort = "80";
json[0].NetworkSettings.SandboxID = "39766f8315f95bb94fe6b801f72aff1fd436f88a5f2417170eeceaa372cb7571";
json[0].NetworkSettings.SandboxKey = "/var/run/docker/netns/39766f8315f9";
json[0].NetworkSettings.SecondaryIPAddresses = null;
json[0].NetworkSettings.SecondaryIPv6Addresses = null;
JSON Please!
You like that is greppable, but would prefer the output in JSON? That’s possible Use the backwards option with
--ungron
.
docker inspect nginx | gron | fgrep "NetworkSettings" | gron --ungron
The JSON data:
[
{
"NetworkSettings": {
"Bridge": "",
"EndpointID": "403b4f8273f3d9e3f0cf15d66a3829bfdb64b2d6fda4f83417cf5831998c1292",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"HairpinMode": false,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"Aliases": null,
"DriverOpts": null,
"EndpointID": "403b4f8273f3d9e3f0cf15d66a3829bfdb64b2d6fda4f83417cf5831998c1292",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAMConfig": null,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"Links": null,
"MacAddress": "02:42:ac:11:00:02",
"NetworkID": "00a571161e8b1042db9eef3a461797f3627cb7ce6e5d573ae546d7123bcdf946"
}
},
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "80"
}
]
},
"SandboxID": "39766f8315f95bb94fe6b801f72aff1fd436f88a5f2417170eeceaa372cb7571",
"SandboxKey": "/var/run/docker/netns/39766f8315f9",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null
}
}
]