shell 中处理json 格式的工具

2022/8/3 5:22:45

本文主要是介绍shell 中处理json 格式的工具,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

jq

jq - Command-line JSON processor

Example:

	$ echo '{"foo": 0}' | jq .
	{
		"foo": 0
	}

eg.json 示例
[
    {
        "Id": "1ae91956f65b52a0f1cd075715b3faff96de1e851bb43fc30bb9276b906ae616",
        "Created": "2022-07-14T03:37:48.922841669Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "mysqld"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 6342,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-07-22T00:57:55.298802065Z",
            "FinishedAt": "2022-07-14T10:02:28.485180654Z"
        },
        "Image": "sha256:9266a7924de3fa18eb03110e204cd01e3472d72af31b48a3760e196ebfa837ee",
        "ResolvConfPath": "/data/docker/containers/1ae91956f65b52a0f1cd075715b3faff96de1e851bb43fc30bb9276b906ae616/resolv.conf",
        "HostnamePath": "/data/docker/containers/1ae91956f65b52a0f1cd075715b3faff96de1e851bb43fc30bb9276b906ae616/hostname",
        "HostsPath": "/data/docker/containers/1ae91956f65b52a0f1cd075715b3faff96de1e851bb43fc30bb9276b906ae616/hosts",
        "LogPath": "/data/docker/containers/1ae91956f65b52a0f1cd075715b3faff96de1e851bb43fc30bb9276b906ae616/1ae91956f65b52a0f1cd075715b3faff96de1e851bb43fc30bb9276b906ae616-json.log",
        "Name": "/con-my57",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "unconfined",
        "ExecIDs": null,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/data/docker/overlay2/3ed787d7b560ea34eb621c0d2982914df0afa55020b6a89924d254d12b08b576-init/diff:/data/docker/overlay2/10ef0e54e1ea1160cc9ba7ba5838e1b92050dce47cb705a055595531c6afbff9/diff:/data/docker/overlay2/11337566e963be8d82dfae23bcb877f4dbd76582f115f30f96a70aac72b5332b/diff:/data/docker/overlay2/ec9751f3467be42e6d807df2dbd650f44ac34a24ace09edd65b5939d91afbff5/diff:/data/docker/overlay2/9964be6f377d59dad7b8a7cbf623c7cf24c692aed994e754c0e99069dda5845e/diff:/data/docker/overlay2/fb717df4c71b0d49c5372716b048d7d274c2ee92f5c91973d4c18fc76ff66607/diff:/data/docker/overlay2/7c6a594e60aa86816f82343466b43480bb8ced9f0d085dbc23980b498112e487/diff:/data/docker/overlay2/d0c4e576a2d756f97ec1024da1479ff317e91077904b9133aef02599f400ecbb/diff:/data/docker/overlay2/1cf8be1b3f5078b5456f7de53284f3176382796f4e99c2bb596ff202de03933f/diff:/data/docker/overlay2/05cb886ee5d9174f95c4224884ba97562a260af51cd3e6236dbeed41cfb0e8cf/diff:/data/docker/overlay2/6f6ca00aca524d7fe9325fc1f3f1e7971420a4509e0a6cae4d5232510f78eff7/diff:/data/docker/overlay2/8a19feb3eb2553b68ba6d8a98a805a52d60db3d13729d31b8aa8b63ac874c3e7/diff:/data/docker/overlay2/362dd29e8c05f85cd753030f5f13a3149c7dd7546a1c4ad17c7864e94ed6a2eb/diff:/data/docker/overlay2/bb149c878a8769fbaf796b5b2e85e4390e756eea7c15efa8a033e8e290dbb831/diff",
                "MergedDir": "/data/docker/overlay2/3ed787d7b560ea34eb621c0d2982914df0afa55020b6a89924d254d12b08b576/merged",
                "UpperDir": "/data/docker/overlay2/3ed787d7b560ea34eb621c0d2982914df0afa55020b6a89924d254d12b08b576/diff",
                "WorkDir": "/data/docker/overlay2/3ed787d7b560ea34eb621c0d2982914df0afa55020b6a89924d254d12b08b576/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/data/configuration/mysql/con-my57.cnf",
                "Destination": "/etc/my.cnf",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "volume",
                "Name": "40f070ff03b039ed1c136f6d2bf8b78ac88ab32d360fd7c0fe2019923ff7979c",
                "Source": "/data/docker/volumes/40f070ff03b039ed1c136f6d2bf8b78ac88ab32d360fd7c0fe2019923ff7979c/_data",
                "Destination": "/var/lib/mysql",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "1ae91956f65b",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "3306/tcp": {},
                "33060/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOSU_VERSION=1.14",
                "MYSQL_MAJOR=5.7",
                "MYSQL_VERSION=5.7.38-1.el7",
                "MYSQL_SHELL_VERSION=8.0.29-1.el7"
            ],
            "Cmd": [
                "mysqld"
            ],
            "Image": "my57:latest",
            "Volumes": {
                "/var/lib/mysql": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "1ac0bc18dce450ba884cdf964bf1bd99c60ddf2150654598a8f2684090177ee5",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "3306/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "13306"
                    },
                    {
                        "HostIp": "::",
                        "HostPort": "13306"
                    }
                ],
                "33060/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/1ac0bc18dce4",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "mynet": {
                    "IPAMConfig": {
                        "IPv4Address": "172.18.0.57"
                    },
                    "Links": null,
                    "Aliases": [
                        "1ae91956f65b"
                    ],
                    "NetworkID": "c793f36398bded71ba34b59ae3ed2d556c9671390479a654d5cb932a0e6d4496",
                    "EndpointID": "cd8cb0ab14c0e1682be8440e43f8dc9b0261d6ff44c278afa6128e472605a6aa",
                    "Gateway": "172.18.0.1",
                    "IPAddress": "172.18.0.57",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:12:00:39",
                    "DriverOpts": null
                }
            }
        }
    }
]
cat eg.json | jq .[0].Id
"1ae91956f65b52a0f1cd075715b3faff96de1e851bb43fc30bb9276b906ae616"


这篇关于shell 中处理json 格式的工具的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程