Jq: Process JSON from the command line

Source: Internet
Author: User

Because some JSON data formats are to be processed recently, after some searches, we finally found jq, a great tool. Jq allows you to operate JSON directly on the command line, including sharding, filtering, conversion, and so on.

Let's use several examples to illustrate jq's functions:

Beautiful print

If we use a text editor to open JSON, it may sometimes look messy,.(Point) the filter can immediately adjust the JSON format.

% jq . soundtag.json

How it looks after it is opened in a text editor

Result displayed with jq

Quick query

With jqkeyAs a keyword to make a quick query of JSON, for example:

% jq .cn soundtag.json

This will only showcnThe value corresponding to the key.

Jq key query also supports chained calling, such:

% jq .cn[0].pNum soundtag.json

MPs queue operations

Anyone familiar with command lines may know that | (pipeline) is a very powerful weapon. Fortunately, jq also provides support for pipelines.

% jq '.cn[] | { pNum }' soundTag.json

Here, we use pipelines to filter and construct pNum objects.

Summary

If you need to process JSON in the command line, I strongly recommend jq. Jq not only meets common general requirements, but also provides powerful functions such as computation, built-in functions, condition comparison, variable declaration, and custom functions. If you are interested in this, you may wish to learn through the official jq manual.

Recommended reading:

Send XML and JSON data asynchronously in Struts

Compiling and code testing of the JSON library in Linux

JQuery retrieves JSON data [$. getJSON method]

Convert form data into JSON strings using jQuery and JSON packages

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.