How to parse JSON after cURL

How to parse JSON after cURL

I am using cURL as a tool for testing REST API — make HTTP/HTTPS requests. In most cases, the response body contains JSON. I assume that the reader knows what cURL (wiki) is for and is familiar with JSON (wiki) format.

All the methods described below will work if the cURL output contains only valid JSON ⇒ do not use -i option (show the HTTP response headers), which will add additional data to output except response’s body like this:

Response headers are included in the cURL output
Response headers are included in the cURL output

Otherwise you will not be able to parse response by any tool:

Invalid JSON is not parseable

For all examples I used macOS Big Sur and its default Terminal; all additional tools can be installed through homebrew.

Python

If you do not want to install any additional tool — Python is your choice, of course, if your OS comes with it. It has a built-in JSON encoder and decoder which can be called from the command line through the pipe:

curl 'https://api.openweathermap.org/data/2.5/onecall?lat=40.1811&lon=44.5136&appid={api_key}' | python -m json.tool

The response will be in human readable form:

| python -m json.tool
| python -m json.tool

Additional reading:

jq

Jq is a flexible command-line JSON processor and the most popular solution:

curl 'https://api.openweathermap.org/data/2.5/onecall?lat=40.1811&lon=44.5136&appid={api_key}' | jq

The response will look nice and with syntax highlighting:

| jq
| jq

The advantage of the tool is an extended filtration system. For example, you can immediately get value of required key:

curl 'https://api.openweathermap.org/data/2.5/onecall?lat=40.1811&lon=44.5136&appid={api_key}' | jq .timezone

| jq .timezone
| jq .json_key

Additional reading:

fx

Fx is a command-line JSON viewer and manipulation tool — after getting the JSON you can navigate through it:

fx navigation through JSON
fx navigation through JSON

The highlighted response can be obtained without interactive mode by . option:

curl 'https://api.openweathermap.org/data/2.5/onecall?lat=40.1811&lon=44.5136&appid={api_key}' | fx .

| fx .
| fx .

Despite that fx is less popular than jq, it also supports filters:

curl 'https://api.openweathermap.org/data/2.5/onecall?lat=40.1811&lon=44.5136&appid={api_key}' | fx .current.humidity

| fx .json_key

Additional reading:

jless

Jless is the newest command-line JSON viewer with a bunch of vim-inspired commands (like :q to exit) for navigation.

After request you will immediately get into the beautiful interactive mode, where you can move and search through JSON:

curl 'https://api.openweathermap.org/data/2.5/onecall?lat=40.1811&lon=44.5136&appid={api_key}' | jless

| jless
| jless

Jless is also a worthy tool to work with local JSON files.

Additional reading:

Earlier this year the maintainer of cURL announced the beginning of implementation of JSON support. I hope in the near future we could work with JSON output out of the cURL’s box.

Additional reading:

--

--

--

Quality assurance engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Introducing MoonRadar

Top 7 Benefits of Using ASP.NET for Web Applications in 2022

The Data Scientist Journey, Chapter 6

Take the lead on technology decisions – sustainable and efficient infrastructures with „Micro…

Common Errors while coding in Swift

An API upgrade post-mortem

Mega-CD Model 2 (Japan) BIOS Download

LFI in the ILIAS e-learning platform

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrey Enin

Andrey Enin

Quality assurance engineer

More from Medium

Spring Boot with Lombok

Rest API

OCI APIGW & JWT for invoking OIC with/without credentials — Part I

Writing a Custom OAuth2 Grant Type in WSO2 Identity Server