Hello Guys! This is my second post of this month. In this post I will be explaining on how to parse JSON using command line. A command-line processor can be used to test or debug JSON responses from server. We can easily feed JSON responses from web server into the command-line JSON processor. To parse JSON response using the command line we can download a software called jq which is a JSON parser.To install it and get it running we need to follow the steps below:-
Step 1
Run the command:- sudo apt-get install jq
Step 2
Run the command:- chmod +x ./jq
Step 3
Run the command:- sudo cp jq /usr/bin
The following is an example of a JSON schema(json.txt):-
{ "name": "Jellyfish", "location": { "street": "F-60 Sec 11", "city": "Noida", "state": "UP", "country": "India" }, "employees": [ { "name": "Vivek", "division": "Engineering" }, { "name": "Aman", "division": "Engineering" }, { "name": "Gaurav", "division": "Marketing" } ] }
To parse a JSON object, type the following command:
$ cat json.txt | jq '.name'
Output:-
"Jellyfish"
To parse a nested JSON object we can use the following:
$ cat json.txt | jq '.location.city'
Output:-
"Noida"
To parse a JSON array:
$ cat json.txt | jq '.employees[0].name'
Output:-
"Vivek"
That was all about JSON.
I hope you liked it. 🙂
Vivek Sadh
Recent Comments