Viewing JSON APIs on Ubuntu with curl + jq ― Extract Only the Required Fields in One Go

Linux・CLI・DevOpsカテゴリを表すパンダのイラスト Linux / CLI / DevOps

About This Article
This article is created using an automated generation workflow leveraging generative AI. We verified the public specifications of curl, jq, and the GitHub REST API, organizing them as tips for quickly reading JSON APIs on Ubuntu.

Verification Status: 📘 Public Specifications Verified · Bash Sample Implemented · Target Ubuntu Physical Machine Unverified

Viewing JSON APIs on Ubuntu with curl + jq ― Extract Only the Required Fields in One Go

While fetching API JSON using curl is straightforward, it can sometimes be long and hard to read as is. On Ubuntu, combining curl and jq allows you to connect fetching and formatting in a single pipe.

Using GitHub’s Public Repository API as an Example

# Fetch JSON from the public API using curl.


# -f: Treat HTTP errors as failures


# -sS: Keep normal output silent, but show errors


# -L: Follow redirects

curl -fsSL "https://api.github.com/repos/papanda925/Daily-Code-Samples" |

  # Pass JSON to jq and extract only the 5 required fields.

  jq '{
    full_name,
    visibility,
    default_branch,
    language,
    updated_at
  }'

Instead of the entire response, only the 5 fields you want to check will be displayed.

What are -f -sS -L for?

  • -f: Makes it harder to treat HTTP errors as successes

  • -sS: Suppresses normal progress display while showing errors

  • -L: Follows redirects

When checking APIs, also pay attention to exit codes to reduce oversights such as “processing continued because JSON was returned even though it was a 404.”

Useful Examples Combining jq

You can narrow things down like .items[] for arrays, select(...) for conditions, and {name, status} for required fields only. “Fixing the fields you look at” is better suited for incident investigation than having a human visually inspect massive JSON.

flowchart LR
    A[curlで取得] --> B[JSON]
    B --> C[jqで抽出]
    C --> D[必要項目だけ表示]

When jq Is Not Installed

sudo apt update
sudo apt install -y jq

On production servers, make sure to check operational rules before adding packages arbitrarily.

GitHub Sample

Official Information & Primary Sources

Document information

Article title
Viewing JSON APIs on Ubuntu with curl + jq ― Extract Only the Required Fields in One Go
Published
Updated
Source
https://papanda925.com/?p=15213&lang=en

License: Text and original figures for which this site holds the relevant rights are available under CC BY 4.0 , unless otherwise noted. This article may include content created or edited with generative AI. If code has a separate license notice or a linked GitHub repository license, that license takes precedence for the code. Quotations, third-party materials, images, and trademarks are excluded from this license. Usage policy

Copied title and URL