首页 笔记 图片 查字 
所属分类:Linux
浏览:112
内容:

# 安装命令

yum install jq


[root@local]#  yum install jq

Loaded plugins: fastestmirror

Determining fastest mirrors

base                                                                                | 3.6 kB  00:00:00     

epel                                                                                | 4.7 kB  00:00:00     

extras                                                                              | 2.9 kB  00:00:00     

mysql-connectors-community                                                          | 2.6 kB  00:00:00     

mysql-tools-community                                                               | 2.6 kB  00:00:00     

mysql57-community                                                                   | 2.6 kB  00:00:00     

updates                                                                             | 2.9 kB  00:00:00     

(1/8): epel/x86_64/group_gz                                                         |  97 kB  00:00:00     

(2/8): epel/x86_64/updateinfo                                                       | 1.0 MB  00:00:00     

(3/8): extras/7/x86_64/primary_db                                                   | 249 kB  00:00:00     

(4/8): epel/x86_64/primary_db                                                       | 7.0 MB  00:00:00     

(5/8): updates/7/x86_64/primary_db                                                  |  17 MB  00:00:00     

(6/8): mysql-tools-community/x86_64/primary_db                                      |  89 kB  00:00:00     

(7/8): mysql-connectors-community/x86_64/primary_db                                 |  93 kB  00:00:00     

(8/8): mysql57-community/x86_64/primary_db                                          | 325 kB  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-2.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-2.el7                   epel                 181 k


Transaction Summary

===========================================================================================================

Install  1 Package (+1 Dependent package)


Total download size: 348 k

Installed size: 1.0 M

Is this ok [y/d/N]: y

Downloading packages:

(1/2): jq-1.6-2.el7.x86_64.rpm                                                      | 167 kB  00:00:00     

(2/2): oniguruma-6.8.2-2.el7.x86_64.rpm                                             | 181 kB  00:00:00     

-----------------------------------------------------------------------------------------------------------

Total                                                                      2.1 MB/s | 348 kB  00:00:00     

Running transaction check

Running transaction test

Transaction test succeeded

Running transaction

  Installing : oniguruma-6.8.2-2.el7.x86_64                                                            1/2 

  Installing : jq-1.6-2.el7.x86_64                                                                     2/2 

  Verifying  : oniguruma-6.8.2-2.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-2.el7                                                                           


Complete!


[root@local]# 

[root@local]# jq

jq - commandline JSON processor [version 1.6]


Usage:  jq [options] <jq filter> [file...]

        jq [options] --args <jq filter> [strings...]

        jq [options] --jsonargs <jq filter> [JSON_TEXTS...]


jq is a tool for processing JSON inputs, applying the given filter to

its JSON text inputs and producing the filter's results as JSON on

standard output.


The simplest filter is ., which copies jq's input to its output

unmodified (except for formatting, but note that IEEE754 is used

for number representation internally, with all that that implies).


For more advanced filters see the jq(1) manpage ("man jq")

and/or https://stedolan.github.io/jq


Example:


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

        {

                "foo": 0

        }


For a listing of options, use jq --help.

[root@local]# 


# 使用例子

[root@local]  vim  test.sh 

jsonstr='{"name":"zhangsan","age":25,"arr":["aaa","bbb","ccc"],"addr":{"province":"js","city":"cz"}}'

echo ${jsonstr} | jq -r '.name'

echo ${jsonstr} | jq -r '.arr'

echo ${jsonstr} | jq -r '.arr[0]'

echo ${jsonstr} | jq -r '.arr[2]'

echo ${jsonstr} | jq -r '.addr.province'

echo ${jsonstr} | jq -r '.addr.city'


jsonstr='[{"id":"001","value":"1"},{"id":"002","value":"2"},{"id":"003","value":"3"},{"id":"004","value":"4"},{"id":"005","value":"5"},{"id":"006","value":"6"}]'

k=0

id=`echo ${jsonstr} | jq  -r ".["${k}"].id"`

value=`echo ${jsonstr} | jq  -r ".["${k}"].value"`

echo "${id}  ${value}"

k=1

id=`echo ${jsonstr} | jq  -r ".["${k}"].id"`

value=`echo ${jsonstr} | jq  -r ".["${k}"].value"`

echo "${id}  ${value}"