linux 中格式化json字符串
用法如下:
[root@iZbp143t3oxhfc3ar7jey0Z 84bd26b94eec4e52a712649207be6468fd4484d824bcfc37ebac5305e1398351]# yum -y install jq Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package jq.x86_64 0:1.6-1.el7 will be installed --> Processing Dependency: libonig.so.2()(64bit) for package: jq-1.6-1.el7.x86_64 --> Running transaction check ---> Package oniguruma.x86_64 0:5.9.5-3.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ========================================================================================================================================================== Package Arch Version Repository Size ========================================================================================================================================================== Installing: jq x86_64 1.6-1.el7 epel 167 k Installing for dependencies: oniguruma x86_64 5.9.5-3.el7 epel 129 k Transaction Summary ========================================================================================================================================================== Install 1 Package (+1 Dependent package) Total download size: 296 k Installed size: 936 k Downloading packages: (1/2): jq-1.6-1.el7.x86_64.rpm | 167 kB 00:00:00 (2/2): oniguruma-5.9.5-3.el7.x86_64.rpm | 129 kB 00:00:00 ---------------------------------------------------------------------------------------------------------------------------------------------------------- Total 2.4 MB/s | 296 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : oniguruma-5.9.5-3.el7.x86_64 1/2 Installing : jq-1.6-1.el7.x86_64 2/2 Verifying : jq-1.6-1.el7.x86_64 1/2 Verifying : oniguruma-5.9.5-3.el7.x86_64 2/2 Installed: jq.x86_64 0:1.6-1.el7 Dependency Installed: oniguruma.x86_64 0:5.9.5-3.el7 Complete!
效果如下:
[root@iZbp143t3oxhfc3ar7jey0Z 84bd26b94eec4e52a712649207be6468fd4484d824bcfc37ebac5305e1398351]# cat config.v2.json | jq . { "StreamConfig": {}, "State": { "Running": false, "Paused": false, "Restarting": false, "OOMKilled": false, "RemovalInProgress": false, "Dead": false, "Pid": 0, "ExitCode": 0, "Error": "", "StartedAt": "2020-03-03T06:28:02.825548595Z", "FinishedAt": "2020-03-03T06:38:25.521104656Z", "Health": null }, "ID": "84bd26b94eec4e52a712649207be6468fd4484d824bcfc37ebac5305e1398351", "Created": "2020-03-03T06:28:01.680148213Z", "Managed": false, "Path": "nginx", "Args": [ "-g", "daemon off;" ], "Config": { "Hostname": "84bd26b94eec", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "80/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "NGINX_VERSION=1.17.8", "NJS_VERSION=0.3.8", "PKG_RELEASE=1~buster" ], "Cmd": [ "nginx", "-g", "daemon off;" ], "ArgsEscaped": true, "Image": "nginx", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>" }, "StopSignal": "SIGTERM" }, "Image": "sha256:a1523e859360df9ffe2b31a8270f5e16422609fe138c1636383efdc34b9ea2d6", "NetworkSettings": { "Bridge": "", "SandboxID": "af07ef432aa54450bb224510e47233ff874f73ab9c5c23e197cc964416801300", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "f9c6306568ccdd1b28efdcdcfa9ab610556441b9d5f1f5710da615eefa955fd5", "EndpointID": "", "Gateway": "", "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "", "IPAMOperational": false } }, "Service": null, "Ports": null, "SandboxKey": "/var/run/docker/netns/af07ef432aa5", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "IsAnonymousEndpoint": false, "HasSwarmEndpoint": false }, "LogPath": "", "Name": "/nginx", "Driver": "overlay2", "MountLabel": "", "ProcessLabel": "", "RestartCount": 0, "HasBeenStartedBefore": true, "HasBeenManuallyStopped": false, "MountPoints": {}, "SecretReferences": null, "AppArmorProfile": "", "HostnamePath": "/var/lib/docker/containers/84bd26b94eec4e52a712649207be6468fd4484d824bcfc37ebac5305e1398351/hostname", "HostsPath": "/var/lib/docker/containers/84bd26b94eec4e52a712649207be6468fd4484d824bcfc37ebac5305e1398351/hosts", "ShmPath": "/var/lib/docker/containers/84bd26b94eec4e52a712649207be6468fd4484d824bcfc37ebac5305e1398351/shm", "ResolvConfPath": "/var/lib/docker/containers/84bd26b94eec4e52a712649207be6468fd4484d824bcfc37ebac5305e1398351/resolv.conf", "SeccompProfile": "", "NoNewPrivileges": false }
没有用的如下:
[root@iZbp143t3oxhfc3ar7jey0Z 84bd26b94eec4e52a712649207be6468fd4484d824bcfc37ebac5305e1398351]# cat config.v2.json {"StreamConfig":{},"State":{"Running":false,"Paused":false,"Restarting":false,"OOMKilled":false,"RemovalInProgress":false,"Dead":false,"Pid":0,"ExitCode":0,"Error":"","StartedAt":"2020-03-03T06:28:02.825548595Z","FinishedAt":"2020-03-03T06:38:25.521104656Z","Health":null},"ID":"84bd26b94eec4e52a712649207be6468fd4484d824bcfc37ebac5305e1398351","Created":"2020-03-03T06:28:01.680148213Z","Managed":false,"Path":"nginx","Args":["-g","daemon off;"],"Config":{"Hostname":"84bd26b94eec","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"ExposedPorts":{"80/tcp":{}},"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","NGINX_VERSION=1.17.8","NJS_VERSION=0.3.8","PKG_RELEASE=1~buster"],"Cmd":["nginx","-g","daemon off;"],"ArgsEscaped":true,"Image":"nginx","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":{"maintainer":"NGINX Docker Maintainers \u003cdocker-maint@nginx.com\u003e"},"StopSignal":"SIGTERM"},"Image":"sha256:a1523e859360df9ffe2b31a8270f5e16422609fe138c1636383efdc34b9ea2d6","NetworkSettings":{"Bridge":"","SandboxID":"af07ef432aa54450bb224510e47233ff874f73ab9c5c23e197cc964416801300","HairpinMode":false,"LinkLocalIPv6Address":"","LinkLocalIPv6PrefixLen":0,"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"f9c6306568ccdd1b28efdcdcfa9ab610556441b9d5f1f5710da615eefa955fd5","EndpointID":"","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"","IPAMOperational":false}},"Service":null,"Ports":null,"SandboxKey":"/var/run/docker/netns/af07ef432aa5","SecondaryIPAddresses":null,"SecondaryIPv6Addresses":null,"IsAnonymousEndpoint":false,"HasSwarmEndpoint":false},"LogPath":"","Name":"/nginx","Driver":"overlay2","MountLabel":"","ProcessLabel":"","RestartCount":0,"HasBeenStartedBefore":true,"HasBeenManuallyStopped":false,"MountPoints":{},"SecretReferences":null,"AppArmorProfile":"","HostnamePath":"/var/lib/docker/containers/84bd26b94eec4e52a712649207be6468fd4484d824bcfc37ebac5305e1398351/hostname","HostsPath":"/var/lib/docker/containers/84bd26b94eec4e52a712649207be6468fd4484d824bcfc37ebac5305e1398351/hosts","ShmPath":"/var/lib/docker/containers/84bd26b94eec4e52a712649207be6468fd4484d824bcfc37ebac5305e1398351/shm","ResolvConfPath":"/var/lib/docker/containers/84bd26b94eec4e52a712649207be6468fd4484d824bcfc37ebac5305e1398351/resolv.conf","SeccompProfile":"","NoNewPrivileges":false} [root@iZbp143t3oxhfc3ar7jey0Z 84bd26b94eec4e52a712649207be6468fd4484d824bcfc37ebac5305e1398351]