Skip to content
Back to skills

Add Json Output

ASecurity

Add --json flag to CLI commands for machine-readable output on stdout. Generic pattern for any CLI tool, enabling piping to jq and CI/CD integration while keeping human-readable output on stderr.

  • 2 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added September 10, 2026
toolsgotestingci/cd

Works with

  • cli

Security analysis

A100/100

Scanned September 10, 2026

npx -y skills add krafteq/infraglue --skill add-json-output --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Add Json Output?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Add Json Output
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/krafteq-add-json-output/badge)](https://www.skillsdirectory.com/skills/krafteq-add-json-output)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: add-json-output
description: Add --json flag to CLI commands for machine-readable output on stdout. Generic pattern for any CLI tool, enabling piping to jq and CI/CD integration while keeping human-readable output on stderr.
---

# Add JSON Output Mode

## Goal

Add `--json` flag to commands that produce output, writing structured JSON to stdout for programmatic consumption.

## Convention

- **stdout**: JSON output (only when `--json` is passed)
- **stderr**: Human-readable output (logs, progress, errors) — always

This allows: `mycli show --json | jq '.items'` to work while the user still sees progress on stderr.

## Implementation

### 1. Add --json Option to Commands

```ts
program
  .command('show')
  .option('--json', 'Output in JSON format')
  .action(async (options) => {
    const result = await buildResult()
    if (options.json) {
      process.stdout.write(JSON.stringify(result, null, 2) + '\n')
    } else {
      displayHumanReadable(result)
    }
  })
```

### 2. Define Result Types

Define typed interfaces for each command's JSON output:

```ts
interface ShowResult {
  items: Array<{
    name: string
    status: string
    // ... domain-specific fields
  }>
}
```

### 3. Suppress Decorative Output in JSON Mode

When `--json` is active, suppress spinners and progress bars on stderr:

```ts
if (options.json) {
  logger.setLevel('warn') // Only show warnings and errors
}
```

### 4. Error Output in JSON Mode

On error with `--json`, still output JSON to stdout:

```ts
if (options.json) {
  process.stdout.write(
    JSON.stringify(
      {
        success: false,
        error: {
          type: error.name,
          message: error.message,
          exitCode: error instanceof AppError ? error.exitCode : 1,
        },
      },
      null,
      2,
    ) + '\n',
  )
}
process.exit(exitCode)
```

### 5. Testing

```ts
test('show --json outputs valid JSON to stdout', async () => {
  const { stdout, stderr } = await runCli(['show', '--json'])
  const result = JSON.parse(stdout)
  expect(result.items).toBeInstanceOf(Array)
  // stderr may still have log output
})
```

## Which Commands

Add `--json` to any command that displays data:

- List/show commands
- Status commands
- Execution result commands
- Config display commands

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.