{"id":256638,"date":"2022-11-24T09:52:44","date_gmt":"2022-11-24T01:52:44","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=256638"},"modified":"2022-11-03T20:53:26","modified_gmt":"2022-11-03T12:53:26","slug":"json-operation-under-linux","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/json-operation-under-linux.html","title":{"rendered":"Linux\u4e0b\u7684json\u6587\u4ef6\u64cd\u4f5c"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\npython\u811a\u672c\u4e2d\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u901a\u5e38\u5c06\u914d\u7f6e\u6587\u4ef6\u653e\u5728json\u683c\u5f0f\u7684\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u505a\u540e\u7eed\u7684\u5904\u7406\uff0c\u90a3\u4e48\u5728linux\u670d\u52a1\u5668\u4e0a\u7f16\u8f91\u548c\u8bfb\u53d6json\u6587\u4ef6\u6709\u4ec0\u4e48\u6280\u5de7\u5462\uff1f\u6765\u770b\u4e00\u4e0b\u5427\uff0c<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

Json\u6570\u636e\u683c\u5f0f\u5316<\/strong><\/div>\n

json\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u5e94\u7528\u8303\u56f4\u975e\u5e38\u5e7f\u6cdb\u3002\u5728Linux\u7cfb\u7edf\u4e0b\u4f7f\u7528jq\u5de5\u5177\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u7684\u5904\u7406json<\/p>\n

\r\n[root@knode1 ~]# cat k8s.conf\r\n{\"hosts\": {\"host_list\": [ {\"hostname\": \"host1\", \"ip\": \"10.1.251.1\", \"username\": \"aps\", \"password\": \"tingsoft\", \"ssh_port\": 22}, {\"hostname\": \"host24\", \"ip\": \"10.1.251.2\", \"username\": \"aps\", ssword\": \"tingsoft\", \"ssh_port\": 22} ], \"sshkey_enable\": false, \"data_dir\": \"\/opt\", \"src\":\"\/etc\/localtime\", \"dest\": \"\/etc\/timezone\", \"port\": \"8080\" } }\r\n<\/pre>\n

\u5728linux\u4e0a\u5b89\u88c5jq\u5de5\u5177<\/p>\n

\r\n[root@knode1 ~]# yum -y install jq\r\n<\/pre>\n

\u5c06\u4e0a\u9762k8s.conf\u6587\u4ef6\u7528json\u683c\u5f0f\u5c55\u793a,\u63d0\u9ad8\u53ef\u8bfb\u6027<\/p>\n

\u65b9\u6cd5\u4e00:cat filename.conf | jq<\/strong><\/span><\/div>\n
\r\n[root@knode1 ~]# cat k8s.conf | jq\r\n{\r\n\"hosts\": {\r\n\"host_list\": [\r\n{\r\n\"hostname\": \"host1\",\r\n\"ip\": \"10.1.251.1\",\r\n\"username\": \"aps\",\r\n\"password\": \"tingsoft\",\r\n\"ssh_port\": 22\r\n},\r\n{\r\n\"hostname\": \"host24\",\r\n\"ip\": \"10.1.251.2\",\r\n\"username\": \"aps\",\r\n\"password\": \"tingsoft\",\r\n\"ssh_port\": 22\r\n}\r\n],\r\n\"sshkey_enable\": false,\r\n\"data_dir\": \"\/opt\",\r\n\"src\": \"\/etc\/localtime\",\r\n\"dest\": \"\/etc\/timezone\",\r\n\"port\": \"8080\"\r\n}\r\n}\r\n<\/pre>\n

jq\u5de5\u5177\u4f1a\u628ajson\u6587\u4ef6\u66f4\u6709\u597d\u7684\u8bfb\u53d6\u51fa\u6765\uff0c\u6b64\u5916\uff0cjq\u5de5\u5177\u8fd8\u5728\u80cc\u540e\u68c0\u67e5json\u6587\u4ef6\u7684\u5408\u6cd5\u6027\uff0c\u5982\u679c\u6587\u4ef6\u5b58\u5728\u683c\u5f0f\u4e0a\u7684\u9519\u8bef\uff0cjq\u4e5f\u4f1a\u62a5\u51fa\u9519\u8bef\u5b58\u5728\u7684\u4f4d\u7f6e<\/p>\n

\u65b9\u6cd5\u4e8c\uff1acat filename.conf | python -m json.tool<\/strong><\/span><\/div>\n

python -m json.tool\u8fd9\u79cd\u65b9\u6cd5\u9700\u8981\u670d\u52a1\u5668\u4e0a\u5b89\u88c5python\uff0c\u6548\u679c\u548cjq\u4e00\u6837\uff0c\u4e5f\u4f1a\u6821\u9a8c\u6570\u636e\u5408\u6cd5\u6027\uff0c\u51fa\u73b0\u683c\u5f0f\u62a5\u9519\u4f1a\u62a5\u51fa\u4f4d\u7f6e<\/p>\n

\r\n[root@knode1 ~]# cat k8s.conf |  python -m json.tool\r\nExpecting object: line 1 column 343 (char 342)\r\n<\/pre>\n
Json\u6570\u636e\u8bfb\u53d6<\/strong><\/div>\n
\u901a\u8fc7Key\u83b7\u53d6Value\u7684\u503c<\/strong><\/span><\/div>\n
\r\n[root@knode1 ~]# cat k8s.conf | jq .hosts\r\n{\r\n\"host_list\": [\r\n{\r\n\"hostname\": \"host1\",\r\n\"ip\": \"10.1.251.1\",\r\n\"username\": \"aps\",\r\n\"password\": \"tingsoft\",\r\n\"ssh_port\": 22\r\n},\r\n{\r\n\"hostname\": \"host24\",\r\n\"ip\": \"10.1.251.2\",\r\n\"username\": \"aps\",\r\n\"password\": \"tingsoft\",\r\n\"ssh_port\": 22\r\n}\r\n],\r\n\"sshkey_enable\": false,\r\n\"data_dir\": \"\/opt\",\r\n\"src\": \"\/etc\/localtime\",\r\n\"dest\": \"\/etc\/timezone\",\r\n\"port\": \"8080\"\r\n}\r\n[root@knode1 ~]# cat k8s.conf | jq .hosts.host_list\r\n[\r\n{\r\n\"hostname\": \"host1\",\r\n\"ip\": \"10.1.251.1\",\r\n\"username\": \"aps\",\r\n\"password\": \"tingsoft\",\r\n\"ssh_port\": 22\r\n},\r\n{\r\n\"hostname\": \"host24\",\r\n\"ip\": \"10.1.251.2\",\r\n\"username\": \"aps\",\r\n\"password\": \"kingsoft\",\r\n\"ssh_port\": 22\r\n}\r\n]\r\n<\/pre>\n

\u89e3\u6790\u4e0d\u5b58\u5728\u7684\u5143\u7d20key\u65f6\uff0c\u4f1a\u8fd4\u56denull,.hosts.host_list\u8fd9\u79cd\u5c5e\u4e8e\u5d4c\u5957\u89e3<\/p>\n

\u81ea\u5b9a\u4e49\u8f93\u51fa\u6570\u7ec4<\/strong><\/span><\/div>\n

\u5bf9\u4e8e\u4e0a\u9762jq .hosts.host_list\u83b7\u53d6\u7684\u7ed3\u679c\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u3010\u3011\u6570\u7ec4\u5f62\u5f0f\u83b7\u53d6<\/p>\n

[root@knode1 ~]# cat k8s.conf | jq .hosts.host_list[0]\r\n{\r\n\"hostname\": \"host1\",\r\n\"ip\": \"10.1.251.1\",\r\n\"username\": \"ups\",\r\n\"password\": \"tingsoft\",\r\n\"ssh_port\": 22\r\n}\r\n<\/pre>\n

\u638c\u63e1\u4e86\u4e0a\u9762\u8fd9\u4e9b\u57fa\u672c\u7684\u6570\u636e\u5904\u7406\u65b9\u6cd5\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728linux\u548cpython\u811a\u672c\u4e2d\u505a\u4e00\u4e9b\u914d\u7f6e\u7684\u57fa\u7840\u5904\u7406\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

json\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u5e94\u7528\u8303\u56f4\u975e\u5e38\u5e7f\u6cdb\u3002\u5728Linux\u7cfb\u7edf\u4e0b\u4f7f\u7528jq\u5de5\u5177\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u7684\u5904\u7406json […]<\/p>\n","protected":false},"author":1329,"featured_media":256642,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-256638","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/256638","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=256638"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/256638\/revisions"}],"predecessor-version":[{"id":256643,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/256638\/revisions\/256643"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/256642"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=256638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=256638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=256638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}