linux下json工具jq
1、查看json文件
[root@VM-1-10-11 f46c19f56252a74a46fd30026001e62cc5ecadd04bc9a80c47f6fd5f9dc0586b]# pwd /data1/docker/containers/f46c19f56252a74a46fd30026001e62cc5ecadd04bc9a80c47f6fd5f9dc0586b [root@VM-1-10-11 f46c19f56252a74a46fd30026001e62cc5ecadd04bc9a80c47f6fd5f9dc0586b]# ls config.v2.json hostconfig.json hostname hosts resolv.conf resolv.conf.hash secrets shm [root@VM-1-10-11 f46c19f56252a74a46fd30026001e62cc5ecadd04bc9a80c47f6fd5f9dc0586b]# [root@VM-1-10-11 f46c19f56252a74a46fd30026001e62cc5ecadd04bc9a80c47f6fd5f9dc0586b]# cat config.v2.json {"StreamConfig":{},"State":{"Running":true,"Paused":false,"Restarting":false,"OOMKilled":false,"RemovalInProgress":false,"Dead":false,"Pid":9173,"StartedAt":"2017-02-09T03:46:15.709079233Z","FinishedAt":"0001-01-01T00:00:00Z","Health":null},"ID":"f46c19f56252a74a46fd30026001e62cc5ecadd04bc9a80c47f6fd5f9dc0586b","Created":"2017-02-08T13:33:52.903241912Z","Managed":false,"Path":"/sbin/init","Args":[],"Config":{"Hostname":"shibing.zhao-162-200","Domainname":"","User":"","AttachStdin":true,"AttachStdout":true,"AttachStderr":true,"Tty":true,"OpenStdin":true,"StdinOnce":true,"Env":["IDC=beebank","CONTAINER_NAME=shibing.zhao_172.16.162.200"],"Cmd":["/sbin/init"],"Image":"migrate.i.beebank.com:5000/shibing.zhao_172.16.162.200","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":{}},"Image":"sha256:a2b61eaadc77aff698d7c9ebc1f0837ee0e5be1942f81b528f7a56e2e3ca257b","NetworkSettings":{"Bridge":"","SandboxID":"8fe25ef4a2a25902d1a56ad1a3a096d24e5955d4045fb4b34429c6575e570bb4","HairpinMode":false,"LinkLocalIPv6Address":"","LinkLocalIPv6PrefixLen":0,"Networks":{"none":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"f989ab193573c45f2d337e5752334afcb5b49d4b97efb2299743c4de3311d97a","EndpointID":"fc95bc1a8306daff737d1dbc135bd107b83226a5e01cdecb8b68b5c7f4dde249","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":""}},"Service":null,"Ports":{},"SandboxKey":"/var/run/docker/netns/8fe25ef4a2a2","SecondaryIPAddresses":null,"SecondaryIPv6Addresses":null,"IsAnonymousEndpoint":false},"LogPath":"","Name":"/shibing.zhao_172.16.162.200","Driver":"devicemapper","MountLabel":"","ProcessLabel":"","RestartCount":0,"HasBeenStartedBefore":false,"HasBeenManuallyStopped":false,"MountPoints":{"/data2":{"Source":"/data1/docker_volume/shibing.zhao_172.16.162.200/data","Destination":"/data2","RW":true,"Name":"","Driver":"","Relabel":"","Propagation":"rprivate","Named":false,"ID":""},"/data3":{"Source":"/data1/docker_volume/user/shibing.zhao","Destination":"/data3","RW":true,"Name":"","Driver":"","Relabel":"","Propagation":"rprivate","Named":false,"ID":""},"/etc/hosts":{"Source":"/data1/docker_volume/shibing.zhao_172.16.162.200/hosts","Destination":"/etc/hosts","RW":true,"Name":"","Driver":"","Relabel":"","Propagation":"rprivate","Named":false,"ID":""},"/etc/mnt":{"Source":"/data1/docker_volume/shibing.zhao_172.16.162.200/config","Destination":"/etc/mnt","RW":false,"Name":"","Driver":"","Relabel":"ro","Propagation":"rprivate","Named":false,"ID":""},"/etc/pam_ldap.conf":{"Source":"/etc/pam_ldap.conf.beebank","Destination":"/etc/pam_ldap.conf","RW":false,"Name":"","Driver":"","Relabel":"ro","Propagation":"rprivate","Named":false,"ID":""},"/etc/resolv.conf":{"Source":"/etc/resolv.conf","Destination":"/etc/resolv.conf","RW":false,"Name":"","Driver":"","Relabel":"ro","Propagation":"rprivate","Named":false,"ID":""},"/etc/yum.repos.d/bee.repo":{"Source":"/data1/docker_volume/beebank.repo","Destination":"/etc/yum.repos.d/bee.repo","RW":false,"Name":"","Driver":"","Relabel":"ro","Propagation":"rprivate","Named":false,"ID":""}},"AppArmorProfile":"","HostnamePath":"/data1/docker/containers/f46c19f56252a74a46fd30026001e62cc5ecadd04bc9a80c47f6fd5f9dc0586b/hostname","HostsPath":"/data1/docker_volume/shibing.zhao_172.16.162.200/hosts","ShmPath":"/data1/docker/containers/f46c19f56252a74a46fd30026001e62cc5ecadd04bc9a80c47f6fd5f9dc0586b/shm","ResolvConfPath":"/etc/resolv.conf","SeccompProfile":"","NoNewPrivileges":false} [root@VM-1-10-11 f46c19f56252a74a46fd30026001e62cc5ecadd04bc9a80c47f6fd5f9dc0586b]#
2、使用jq解析json文件内容
[root@VM-1-10-11 f46c19f56252a74a46fd30026001e62cc5ecadd04bc9a80c47f6fd5f9dc0586b]# cat config.v2.json |jq { "StreamConfig": {}, "State": { "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "RemovalInProgress": false, "Dead": false, "Pid": 9173, "StartedAt": "2017-02-09T03:46:15.709079233Z", "FinishedAt": "0001-01-01T00:00:00Z", "Health": null }, "ID": "f46c19f56252a74a46fd30026001e62cc5ecadd04bc9a80c47f6fd5f9dc0586b", "Created": "2017-02-08T13:33:52.903241912Z", "Managed": false, "Path": "/sbin/init", "Args": [], "Config": { "Hostname": "shibing.zhao-162-200", "Domainname": "", "User": "", "AttachStdin": true, "AttachStdout": true, "AttachStderr": true, "Tty": true, "OpenStdin": true, "StdinOnce": true, "Env": [ "IDC=beebank", "CONTAINER_NAME=shibing.zhao_172.16.162.200" ], "Cmd": [ "/sbin/init" ], "Image": "migrate.i.beebank.com:5000/shibing.zhao_172.16.162.200", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": {} }, "Image": "sha256:a2b61eaadc77aff698d7c9ebc1f0837ee0e5be1942f81b528f7a56e2e3ca257b", "NetworkSettings": { "Bridge": "", "SandboxID": "8fe25ef4a2a25902d1a56ad1a3a096d24e5955d4045fb4b34429c6575e570bb4", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Networks": { "none": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "f989ab193573c45f2d337e5752334afcb5b49d4b97efb2299743c4de3311d97a", "EndpointID": "fc95bc1a8306daff737d1dbc135bd107b83226a5e01cdecb8b68b5c7f4dde249", "Gateway": "", "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "" } }, "Service": null, "Ports": {}, "SandboxKey": "/var/run/docker/netns/8fe25ef4a2a2", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "IsAnonymousEndpoint": false }, "LogPath": "", "Name": "/shibing.zhao_172.16.162.200", "Driver": "devicemapper", "MountLabel": "", "ProcessLabel": "", "RestartCount": 0, "HasBeenStartedBefore": false, "HasBeenManuallyStopped": false, "MountPoints": { "/data2": { "Source": "/data1/docker_volume/shibing.zhao_172.16.162.200/data", "Destination": "/data2", "RW": true, "Name": "", "Driver": "", "Relabel": "", "Propagation": "rprivate", "Named": false, "ID": "" }, "/data3": { "Source": "/data1/docker_volume/user/shibing.zhao", "Destination": "/data3", "RW": true, "Name": "", "Driver": "", "Relabel": "", "Propagation": "rprivate", "Named": false, "ID": "" }, "/etc/hosts": { "Source": "/data1/docker_volume/shibing.zhao_172.16.162.200/hosts", "Destination": "/etc/hosts", "RW": true, "Name": "", "Driver": "", "Relabel": "", "Propagation": "rprivate", "Named": false, "ID": "" }, "/etc/mnt": { "Source": "/data1/docker_volume/shibing.zhao_172.16.162.200/config", "Destination": "/etc/mnt", "RW": false, "Name": "", "Driver": "", "Relabel": "ro", "Propagation": "rprivate", "Named": false, "ID": "" }, "/etc/pam_ldap.conf": { "Source": "/etc/pam_ldap.conf.beebank", "Destination": "/etc/pam_ldap.conf", "RW": false, "Name": "", "Driver": "", "Relabel": "ro", "Propagation": "rprivate", "Named": false, "ID": "" }, "/etc/resolv.conf": { "Source": "/etc/resolv.conf", "Destination": "/etc/resolv.conf", "RW": false, "Name": "", "Driver": "", "Relabel": "ro", "Propagation": "rprivate", "Named": false, "ID": "" }, "/etc/yum.repos.d/bee.repo": { "Source": "/data1/docker_volume/beebank.repo", "Destination": "/etc/yum.repos.d/bee.repo", "RW": false, "Name": "", "Driver": "", "Relabel": "ro", "Propagation": "rprivate", "Named": false, "ID": "" } }, "AppArmorProfile": "", "HostnamePath": "/data1/docker/containers/f46c19f56252a74a46fd30026001e62cc5ecadd04bc9a80c47f6fd5f9dc0586b/hostname", "HostsPath": "/data1/docker_volume/shibing.zhao_172.16.162.200/hosts", "ShmPath": "/data1/docker/containers/f46c19f56252a74a46fd30026001e62cc5ecadd04bc9a80c47f6fd5f9dc0586b/shm", "ResolvConfPath": "/etc/resolv.conf", "SeccompProfile": "", "NoNewPrivileges": false }
3、获取指定的key对应的value
[root@BASE-SERVER-1-10-10 81f7f11a8b03397d8153fdaf975a78157afee9b2279607132711e0234dbc5f3a]# cat config.v2.json |jq .State { "Running": true, "Paused": false, "Restarting": false, "OOMKilled": true, "RemovalInProgress": false, "Dead": false, "Pid": 114836, "StartedAt": "2017-02-27T09:36:00.772375786Z", "FinishedAt": "2017-02-27T09:35:41.564086153Z", "Health": null } [root@BASE-SERVER-1-10-10 81f7f11a8b03397d8153fdaf975a78157afee9b2279607132711e0234dbc5f3a]# cat config.v2.json |jq .State.Running true