Shell脚本解析json文件

前言

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。

安装jq

yum -y install jq

结果输出:

Loaded plugins: fastestmirror
Determining fastest mirrors
epel/x86_64/metalink                                                                                                                        | 8.4 kB  00:00:00     
 * base: mirrors.ustc.edu.cn
 * epel: mirror.sjtu.edu.cn
 * extras: mirrors.ustc.edu.cn
 * updates: mirror.biznetgio.com
base                                                                                                                                        | 3.6 kB  00:00:00     
epel                                                                                                                                        | 4.7 kB  00:00:00     
extras                                                                                                                                      | 2.9 kB  00:00:00     
updates                                                                                                                                     | 2.9 kB  00:00:00     
(1/3): epel/x86_64/updateinfo                                                                                                               | 1.0 MB  00:00:00     
(2/3): epel/x86_64/primary_db                                                                                                               | 6.9 MB  00:00:00     
(3/3): updates/7/x86_64/primary_db                                                                                                          | 9.5 MB  00:00:01     
Resolving Dependencies
--> Running transaction check
---> Package jq.x86_64 0:1.6-2.el7 will be installed
--> Processing Dependency: libonig.so.5()(64bit) for package: jq-1.6-2.el7.x86_64
--> Running transaction check
---> Package oniguruma.x86_64 0:6.8.2-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===================================================================================================================================================================
 Package                                 Arch                                 Version                                     Repository                          Size
===================================================================================================================================================================
Installing:
 jq                                      x86_64                               1.6-2.el7                                   epel                               167 k
Installing for dependencies:
 oniguruma                               x86_64                               6.8.2-1.el7                                 epel                               181 k

Transaction Summary
===================================================================================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 348 k
Installed size: 1.0 M
Downloading packages:
(1/2): jq-1.6-2.el7.x86_64.rpm                                                                                                              | 167 kB  00:00:00     
(2/2): oniguruma-6.8.2-1.el7.x86_64.rpm                                                                                                     | 181 kB  00:00:00     
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                              499 kB/s | 348 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : oniguruma-6.8.2-1.el7.x86_64                                                                                                                    1/2 
  Installing : jq-1.6-2.el7.x86_64                                                                                                                             2/2 
  Verifying  : oniguruma-6.8.2-1.el7.x86_64                                                                                                                    1/2 
  Verifying  : jq-1.6-2.el7.x86_64                                                                                                                             2/2 

Installed:
  jq.x86_64 0:1.6-2.el7                                                                                                                                            

Dependency Installed:
  oniguruma.x86_64 0:6.8.2-1.el7                                                                                                                                   

Complete!

使用方法

假设有个test.json文件

{
  "menu": {
    "id": "file",
    "value": "File:",
    "popup": {
      "menuitem": {
        "value": "New",
        "onclick": "CreateNewDoc()"
      }
    }
  }
}

现在要通过shell获取到 menu.id 的内容

#! /bin/bash
 
JQ_EXEC=`which jq`

FILE_PATH=test.json

id=$(cat $FILE_PATH | ${JQ_EXEC} .menu.id | sed 's/\"//g')
echo $id 

如果要继续取下一层数据的内容 把命令改成

id=$(cat $FILE_PATH | ${JQ_EXEC} .menu.popup.menuitem | sed 's/\"//g')

你可能感兴趣的:(Shell脚本解析json文件)