Stop 05 · Scripting & Programming for DevOpsTopics · 3/8

Working With APIs & JSON

Part of the DevOps Roadmap

In short

GitHub, AWS, Slack and Kubernetes all have APIs that speak JSON. Once your scripts can call an API and pick fields out of the reply, you can automate almost anything.

Most modern infrastructure and cloud tools are controlled via REST APIs returning JSON — comfort parsing JSON and making authenticated API calls from a script is a core automation skill.

The mascot as a waiter carries a steel thali with neat separate compartments of food.

Think of it like this

Ordering a thali

You tell the waiter 'one veg thali'. He brings a steel plate with fixed compartments: dal here, sabzi there, rotis on the side. You always know where each item sits. An API is the waiter, and JSON is the thali: every value has its own labelled spot.

Watch it happen

Inside one JSON response

{ }
name: "api"
replicas: 3
tags: [ ]
"prod"
"v1.4"

1/4The whole reply is one JSON object, wrapped in curly braces.

The big ideas

What you need to know

Authenticate with tokens

Most APIs want a header like Authorization: Bearer <token>. Read the token from an environment variable.

jq is sed for JSON

jq '.items[].name' pulls fields out of JSON in the terminal. In Python, use json or response.json().

Check status codes

2xx means success, 4xx means your request was wrong, 5xx means the server failed. Handle each differently.

Respect rate limits

APIs limit how often you call them. On a 429 response, wait and retry instead of hammering.

See it in code

Walk through the code

bash
1# Call an API with a token, read JSON back2curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \3  "https://api.github.com/repos/OWNER/REPO/pulls?state=open" \4  | jq -r '.[] | "\(.number) \(.user.login): \(.title)"'5 6# Pick fields out of JSON with jq7echo '{"name":"api","replicas":3,"tags":["prod","v1.4"]}' > svc.json8jq '.replicas' svc.json        # 39jq -r '.tags[0]' svc.json      # prod10 11# Send JSON in a POST request12curl -s -X POST -H "Content-Type: application/json" \13  -d '{"text":"Deploy finished"}' "$SLACK_WEBHOOK_URL"

1/3Call GitHub's API with a token and print number, author and title of each open PR.

jq -r prints raw strings without quotes, which is what you want when feeding values into other commands.

Save yourself the pain

Common mistakes

Oops

Parsing JSON with grep and cut, which breaks when the field order or spacing changes.

Do this instead

Use a real JSON parser: jq in the shell, or the json module in Python.

Oops

Pasting an API token directly into the script and committing it.

Do this instead

Read tokens from environment variables or a secret manager, and give them the smallest scope possible.

Your mission

Talk to a real API

~25 min

Quick check

An API returns HTTP 429. What should your script do?

Go deeper

  1. 1Use curl or a script to call a free public API and parse the JSON response.
  2. 2Learn to use a tool like jq to extract specific fields from JSON on the command line.
  3. 3Practice handling API authentication (API keys, tokens) securely, not hardcoded in scripts.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge