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

ライセンス:本記事のテキスト/コードは特記なき限り CC BY 4.0 です。引用の際は出典URL(本ページ)を明記してください。
利用ポリシー もご参照ください。

コメント

Copied title and URL