CI/CD 2026-03-10

Validate API Responses in GitHub Actions with JSON Formatter

Automatically validate JSON structure, detect malformed payloads, and pretty-print API responses in GitHub Actions CI/CD pipelines. Includes ready-to-use workflow YAML.

⚙️ Uses: JSON Formatter — Free

The Problem

Your CI/CD pipeline calls an external API and uses the JSON response in downstream steps. If the API returns malformed JSON or unexpected structure, your workflow silently fails with cryptic errors instead of a clear validation message.

Why This Matters

Validating JSON at the CI boundary catches API contract breaks early. A 5-second validation step in your pipeline prevents 30-minute debugging sessions when a downstream tool fails because it received <code>null</code> instead of an object. JSON validation is also required for SOC2 and PCI compliance audit trails.

Step-by-Step Instructions

1

Use the JSON Formatter to inspect your API payload

Paste a sample API response into the formatter below. This reveals structure issues — missing fields, wrong types, unexpected nulls — before you write a single line of CI code.

2

Add a validation step with <code>jq</code> in your workflow

jq is pre-installed on all GitHub-hosted runners. Use jq -e '.field // empty' to assert required fields exist. Exit code 1 if validation fails, stopping the pipeline.

3

Extract values for downstream steps

After validation, extract specific values with jq -r '.token' and write them to $GITHUB_OUTPUT for use in later steps.

4

Log pretty-printed JSON for debugging

Add a jq . step to print formatted JSON in the Actions log. When something breaks, you'll see the exact response instead of a minified one-liner.

Try It Now — JSON Formatter

Open full page →
JSON Formatter — Live Demo

All processing happens in your browser — no data is sent to any server.

Before & After Example

Problem: silent failure when API returns unexpected JSON
# Workflow fails with cryptic error:
- name: Call API
  run: |
    RESPONSE=$(curl -s https://api.example.com/data)
    TOKEN=$(echo $RESPONSE | python3 -c "import sys,json; print(json.load(sys.stdin)['token'])")
    # Fails with: KeyError: 'token'
    # But you don't know WHY — was it a 500? rate limit? null response?
Solution: validate JSON structure before extracting values
- name: Call API and validate response
  id: api_call
  run: |
    RESPONSE=$(curl -s -w "\n%{http_code}" https://api.example.com/data)
    HTTP_CODE=$(echo "$RESPONSE" | tail -1)
    BODY=$(echo "$RESPONSE" | head -1)

    # Validate HTTP status
    if [ "$HTTP_CODE" != "200" ]; then
      echo "API returned $HTTP_CODE"
      exit 1
    fi

    # Validate JSON structure with jq
    echo "$BODY" | jq -e '
      .token != null and
      .expires_at != null and
      (.status == "ok")
    ' || { echo "Invalid API response structure"; echo "$BODY" | jq .; exit 1; }

    # Extract values for downstream steps
    echo "token=$(echo $BODY | jq -r '.token')" >> $GITHUB_OUTPUT
    echo "expires=$(echo $BODY | jq -r '.expires_at')" >> $GITHUB_OUTPUT

- name: Use validated token
  run: echo "Token: ${{ steps.api_call.outputs.token }}"

Frequently Asked Questions

Is <code>jq</code> available on all GitHub Actions runners?

Yes. jq is pre-installed on ubuntu-latest, macos-latest, and windows-latest GitHub-hosted runners. You do not need to install it.

How do I validate nested JSON fields?

Use jq -e '.data.user.id != null' for nested access. For arrays: jq -e '.items | length > 0' asserts the array is non-empty.

Can I validate a JSON schema (not just presence)?

For strict schema validation, use ajv-cli (npm install -g ajv-cli) in your workflow. It validates against a JSON Schema file and outputs clear error messages.

Related Workflows

Want the full JSON Formatter experience?

Open the standalone tool for more space, keyboard shortcuts, and additional features.

Open JSON Formatter →

Related Workflow Guides