Use the jq tool to process JSON data in Shell command lines.

Source: Internet
Author: User

Use the jq tool to process JSON data in Shell command lines.

Because I recently want to process some JSON data formats, after some searches early in the morning, I finally found jq, a great tool. Jq allows you to operate JSON directly on the command line, including sharding, filtering, conversion, and so on.

Install jq on mac and use brew install jq. If homebrew is installed on ubuntu, you can use sudo apt-get install jq to install jq.

Let's use several examples to illustrate jq's functions:
I. Output formatting and beautiful printing results
If we use a text editor to open JSON, it may sometimes look messy, but the JSON format can be adjusted immediately through the jq. (DOT) filter.
1. How it looks after it is opened in a text editor

2. Use the result displayed by jq to check json in the terminal!

$ 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-contrib-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-htmlmin": "~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"}


Ii. Quick query of JSON data

Jq can be used as a keyword to quickly query JSON. For example:

$ jq .version package.json "0.0.0"

Jq key query also supports chained calling, such:

$ jq .scripts.test package.json "grunt test"


3. jq 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 '.devDependencies|{ karma }' package.json {  "karma": "^0.12.19"}

Here, we use pipelines to filter and construct the karma object.


Iv. 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. For more information, see the official jq manual.
What is the difference between the shell command line and the input command in the DOS window?

The difference is big.
Shell is essential for linux. It is not required in theory, but it is actually required.
DOS is an operating system. It was born before win, and now the command line window on win is CMD. It is just a software running rule. scripts are batch processed, but the syntax is incomplete. Logic is flawed. For win, this is not used in most cases. In a few cases, it is the kind that cannot be lost, but it won't make you suffer. However, the shell script is complete and the logic is rigorous.
Powershell is an integrated tool developed by Microsoft on the server side that uses the command line management system. The target is shell, but the reality is far behind, and few people use it.

What should I do if I enter the complete shell command line in the USB flash drive system?

Baidu xianyu pear cloud. A good mass production-free tool. Back up personal data. The specific use method is also Baidu... When the software is running, there should be...

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.