Using the JQ tool to process JSON data on the shell command line

Source: Internet
Author: User

Because of the recent processing of some JSON data format, the early morning after a search, finally found the JQ this great tool. JQ allows you to manipulate JSON directly at the command line, including sharding, filtering, transformations, and so on.

First install JQ under the Mac, use brew install JQ, if installed homebrew, if under Linux Ubuntu, you should be able to use the sudo apt-get install JQ installation.

Let's take a few examples to illustrate the function of JQ:
First, the output format, beautiful printing effect
If we open the JSON with a text editor, it may sometimes seem like a mess, but with JQ's. (dot) filter, the JSON format can be immediately normalized.
1. After opening with a text editor

2. With the results of JQ display, later in the terminal to see JSON comfortable!

$ JQ.    Package.json {"Scripts": {"test": "Grunt Test"}, "engines": {"node": ">=0.10.0"}, "Devdependencies": { "Karma-ng-html2js-preprocessor": "^0.1.0", "Karma": "^0.12.19", "Grunt-karma": "^0.8.3", "Karma-ng-scenario": "^0.1.0", "Time-grunt": "~0.2.1", "Load-grunt-tasks": "~0.4.0", "Jshint-stylish": "~0.1.3", "Grunt-usemin": "~    2.0.0 "," Grunt-svgmin ":" ~0.2.0 "," Grunt-rev ":" ~0.1.0 "," Grunt-ngmin ":" ~0.0.2 "," Grunt-newer ":" ~0.6.1 ", "Grunt-contrib-connect": "~0.5.0", "Grunt-contrib-concat": "~0.3.0", "Grunt-contrib-compass": "~0.7.2", "Grunt-co Ntrib-clean ":" ~0.5.0 "," grunt-concurrent ":" ~0.5.0 "," Grunt-bower-install ":" ~1.0.0 "," Grunt-autoprefixer ":" ~0. " 4.0 "," Grunt ":" ~0.4.1 "," grunt-contrib-copy ":" ~0.4.1 "," Grunt-contrib-cssmin ":" ~0.7.0 "," grunt-contrib-html Min ":" ~0.1.3 "," Grunt-contrib-imagemin ":" ~0.3.0 "," Grunt-contrib-jshint ":" ~0.7.1 "," grunt-contrib-uglify ":" ~0 .2.0 "," grunt-Contrib-watch ":" ~0.5.2 "," Grunt-google-cdn ":" ~0.2.0 "}," dependencies ": {}," version ":" 0.0.0 "," name ":" Myyo "} 


Second, fast query JSON data

The use of JQ allows you to quickly query JSON using key as a keyword, for example:

$ JQ. Version Package.json "0.0.0"

the key query for JQ also supports chained calls, such as:

$ JQ. Scripts.test Package.json "Grunt test"


Three, JQ pipe operation

Friends who are familiar with the command line may know | (plumbing) is a very powerful weapon. Fortunately, JQ also provides support for pipelines.

$ Jq '. devdependencies| {Karma} ' Package.json {  "karma": "^0.12.19"}

Here we use the pipeline to filter and construct the Karma object


Four, summary
If you need to process JSON under the command line, I highly recommend JQ. JQ is not only able to meet the general requirements of common, but also includes operations, built-in functions, condition comparisons, variable declarations, custom functions and other powerful features. More information can be learned through the official JQ manual.

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.