JQ and you!
1 min readJul 27, 2020
Tool for parsing JSON objects from CLI
If you work with CLI tools dealing with API calls, chances are you might have been dumped with huge payload response; and want to pick selected parts of it. Enter JQ —JSON object parser CLI utility using JMESPath syntax. This is a game-changer if the API call doesn’t support native JMESPath syntax to fetch selected objects. If you use AWS CLI, I have a few examples below:
Getting started
- Install JQ — https://stedolan.github.io/jq/download/
- Start parsing JSON objects with jq!
Basics:
- Get JSON data from curl and parse it with jq:
curl -s https://httpbin.org/get | jq .{
"args": {},
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/7.63.0",
"X-Amzn-Trace-Id": "Root=1-5ead7fda-d6634b572db4237f4485907d"
},
"origin": "109.78.73.8",
"url": "https://httpbin.org/get"
}
- Get length of the object:
curl -s https://httpbin.org/get | jq length4
- Get all keys from the response:
curl -s https://httpbin.org/get | jq keys
[
"args",
"headers",
"origin",
"url"
]
- Get the value of all keys from the response:
curl -s https://httpbin.org/get | jq ".[]"
{}
{
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/7.61.1",
"X-Amzn-Trace-Id": "Root=1-5ead8a59-8ec68bb6ff6d7a58d2883c54"
}
"54.212.64.144"
"https://httpbin.org/get"
- Get all headers values from a specific key:
curl -s https://httpbin.org/get | jq ".headers"{
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/7.61.1",
"X-Amzn-Trace-Id": "Root=1-5ead897c-654f503422187fb4553e1d20"
}
- Get values from multiple keys:
curl -s https://httpbin.org/get | jq ".headers,.url"
{
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/7.61.1",
"X-Amzn-Trace-Id": "Root=1-5ead8b0f-702f31eead4dd61cba35d6ea"
}
"https://httpbin.org/get"