Command line tool for handling JSON under Linux: JQ — installation

JSON is often used in front-end programming. There is also an artifact to deal with JSON under Linux: JQ.
It’s just like jbind/SEQ, which is a convenient tool for jbind.

In this article, let’s look at the installation of JQ.

1. Execute Yum list | grep JQ to check if there is a JQ installation package.

2. If yes, install JQ directly, and execute the command: Yum – y install JQ.

After the installation, enter JQ directly on the command line, and then press enter to see the following information to indicate that the installation is completed.

3. If the following error is reported during installation, it may be that the referenced package is not in the existing package source. You can try to switch the package source.

4. Common sources:

(1) Yum source of China University of science and technology:
WGet http://centos.ustc.edu.cn/CentOS-Base.repo
(2) Yum source of 163:
WGet http://mirrors.163.com/.help/CentOS-Base-163.repo
Download the installation package of Yum. Due to different architectures and package updates, the version number of directory and file name may need to be adjusted as follows.
#wget http://mirrors.163.com/centos/5/os/x86_ 64/CentOS/yum-3.2.22-40.el5. centos.noarch.rpm
#wget http://mirrors.163.com/centos/5/os/x86_ 64/CentOS/yum-fastestmirror-1.1.16-21.el5. centos.noarch.rpm
#wget http://mirrors.163.com/centos/5/os/x86_ 64/CentOS/yum-metadata-parser-1.1.2-4.el5.x86_ 64. RPM
(3) Yum source of Sohu:
WGet http://mirrors.sohu.com/help/CentOS-Base-sohu.repo

5. When I installed, none of the above sources worked. Maybe I didn’t find a suitable source, and finally I used the EPEL source. See the previous article for the installation of EPEL.

JSON is a format often used in front-end programming. For PHP or python, parsing JSON is not a big deal, especially for PHP JSON_ Encode and JSON_ Decode, pretty good job. There is also an artifact to deal with JSON under Linux: JQ.
For the JSON format, JQ is as convenient as sed/awk/grep, and JQ has no messy dependencies. It only needs a binary file JQ. Let’s take a look at the use of JQ.
1 format JSON

manu@manu :~/code/php/json$ cat json_ raw.txt

{“name”:”Google”,”location”:{“street”:”1600 Amphitheatre Parkway”,”city”:”Mountain View”,”state”:”California”,”country”:”US”},”employees”:[{“name”:”Michael”,”division”:”Engineering”},{“name”:”Laura”,”division”:”HR”},{“name”:”Elise”,”division”:”Marketing”}]}

The JSON above is PHP JSON_ After encode, echo comes out of the string. Obviously, the readability is too poor. A while ago, when I wrote a document, I needed to write the front and back sections of JSON to the document. I used the JSON formatting tool on the Internet at that time. In fact, JQ can check the validity of JSON and format it into a more friendly and readable format

cat json_ raw.txt |jq.

A289885427

see the figure above, and format a mess of JSON into a more readable form. In fact, the legality of JSON is also checked. If JSON is illegal, JQ. Will report an error. I intentionally wrote a wrong JSON:

manu@manu :~/code/php/json$ cat json_ err.txt

{“name”:”Google”,”location”:{“street”:”1600 Amphitheatre Parkway”,”city”:”Mountain View”,”state”:”California”,”country”:”US”},”employees”:[{“name”:”Michael”,”division”:“Engineering”}{“name”:”Laura”,”division”:”HR”},{“name”:”Elise”,”division”:”Marketing”}]}

In the bold and italicized part of the above JSON, a comma is missing, so this JSON is wrong. JQ can easily check it out

manu@manu :~/code/php/json$ cat json_ err.txt |jq.

parse error:Expected separator between values at line 1,column 183

2 JSON parse
as shown in the figure above, how does JQ parse JSON and get value according to key?

{

“key_ 1”:”value_ 1″,

“key_ 2”:”value_ 2″,

}

How to get value based on key?

jq’.key’

A289885427
parsing nonexistent elements will return null

echo'{“foo”: 42, “bar”: “less interesting data”}’| jq.nofoo

null

3 JSON nested parse

cat json_ raw.txt |jq’. location.state ‘

“California”

4 JSON parse array

cat json_ raw.txt |jq’.employees[1].name’

“Laura”

5 built in functions
JQ and some built-in functions such as key, has
key are used to obtain key elements in JSON

cat json_ raw.txt |jq’keys’

[

“employees”,

“location”,

“name”

]

Has is used to determine whether a key exists

cat json_ raw.txt |jq’has(“name”)’

true

cat json_ raw.txt |jq’has(“noexisted”)’

false

I’m going to introduce some of those related to JSON for the time being. I hope I can learn more about JQ and use it http://stedolan.github.io/jq/manual/ , hope to understand the source code implementation, you can go to https://github.com/stedolan/jq The expected thing is that the author uses flex and bison to parse JSON. Our example JSON comes from the first article in the reference. Using Google search, there is an article in kernalpanic that introduces jshon and json.sh It provides another way of thinking. However, I like and

References:
1 how to parse JSON string via command line on Linux
2 J Q – command line JSON processor

Similar Posts: