Skip to content

The Deska blog

Machine-Readable CLI Output: JSON Flags Done Right

Learn how to implement machine-readable CLI output using JSON flags to improve developer experience and automation workflows.

12 min read

Modern terminal tools are no longer just for human eyes. As developers increasingly build automated pipelines and custom integrations, the need for machine-readable CLI output has become a standard requirement. When a tool outputs plain text intended for a human to read, any script attempting to parse that data becomes fragile. A single change in spacing or a new decorative header can break a regex pattern. Implementing a formal structured output mode, typically via a --json flag, ensures that your tool remains stable and predictable within a larger ecosystem of scripts and automation frameworks.

The Problem with Human Centric Output

Standard terminal output focuses on legibility, using colors, tables, and nested indentation to help a person understand status. While this is excellent for interactive use, it is a nightmare for automation. Developers often resort to tools like awk, sed, or complex regular expressions to scrape the specific data they need from a command.

This approach is brittle for several reasons. First, the visual layout of a CLI tool is subject to change. A developer might decide to add a new column to a table or change a label from "ID" to "UUID," which immediately breaks any script relying on position or string matching. Second, terminal escape codes used for colors can pollute the output stream, making parsing even more difficult unless specifically handled. Structured data solves these issues by decoupling the data model from its visual representation.

Designing the JSON Flag

When adding machine-readable capabilities to your tool, the most common pattern is the --json flag. This should be an opt-in feature that changes the output format entirely.

Consistent Schema

The most important rule for structured output is consistency. Every time a specific command is run with the JSON flag, the root object should follow a predictable schema. If a command usually returns a list of items, the JSON output should always be an array, even if there is only one result or zero results. This prevents "type switching" errors in languages like Go or TypeScript where the parser expects a specific structure.

Error Handling in Structured Output

A common mistake is returning human readable error text to stderr while returning an empty JSON object to stdout. When the machine-readable flag is active, errors should ideally be part of a structured response. This might include an error code, a machine-friendly message, and a stack trace if applicable. By keeping the error inside the JSON structure, you allow the calling script to handle failures gracefully using the same parsing logic it uses for success states.

Comparing Output Strategies

Different tools take different approaches to how they present data. The following table compares common methods for handling CLI data for both humans and machines.

ApproachHuman LegibilityMachine ParsabilityImplementation Effort
Plain Text OnlyHighLowLow
Greppable (Key=Value)MediumMediumMedium
Dedicated JSON FlagLowHighMedium
Auto-detecting TTYHighHighHigh

While auto-detecting a TTY (Teletype) to switch between formats is clever, it can lead to confusion. A developer might pipe output to another tool and find the format has changed unexpectedly. Providing an explicit flag is generally considered a better practice for developer experience.

Integrating with Modern Workspaces

The utility of structured output becomes clear when using an infinite canvas like Deska. When you run tools that provide structured data, the environment can better interpret what is happening. Deska allows you to place multiple terminals side by side. If one terminal is running a long process with JSON output, another panel can potentially ingest that data to provide a visual representation.

Since Deska is local-first, the data generated by your CLI tools stays on your machine. This is critical when your CLI output contains sensitive information like API keys or internal IP addresses. You can use the notes panel to document the specific JSON schemas of your internal tools right next to the terminal where you execute them.

Advanced Usage: Streaming JSON

For long running tasks, returning a single massive JSON object at the end is often insufficient. In these cases, newline-delimited JSON (NDJSON) is a better choice. Each line of output is a complete, valid JSON object. This allows the consumer to process data in real time as it is produced.

This streaming approach is particularly useful for:

  • Build logs where each step is an object.
  • Monitoring tools that emit status updates every few seconds.
  • Large data migrations where progress needs to be tracked.

If you are using coding agents within Deska, such as Claude Code or Codex CLI, these agents are much more effective when they can interact with tools that provide structured data. An agent can parse a JSON response with 100% accuracy, whereas it might hallucinate or misinterpret data from a messy human-readable table.

Deska as a Developer Environment

Deska provides a unique way to manage these complex CLI workflows. Unlike a standard terminal emulator, Deska is a free desktop app that organizes your tools spatially. You can have a dedicated area of your canvas for your automation scripts, with the browser open to the API documentation and a terminal running your tool with the JSON flag enabled.

The Ask Deska assistant can even help you manage these sessions. You can use voice commands to ask Deska to run a specific command or to rearrange your panels to better see the output. If you are away from your desk, the mobile app allows you to monitor those running processes through a secure relay, ensuring you always have visibility into your automation.

FAQ

How do I handle big JSON outputs in the terminal?

When a tool outputs a large amount of JSON, it is best to pipe the result into a tool like jq. This allows you to filter, transform, and prettify the data. Inside Deska, you can keep a jq command ready in a note panel to quickly copy and paste it when needed.

Should JSON output include colors?

No. Machine-readable output should be clean of ANSI escape codes. These codes are meant for terminal emulators to render colors and will cause parsing errors in most JSON libraries. If you must support color in JSON, it should be a separate, explicit flag.

Is JSON better than YAML for CLI output?

JSON is generally preferred because it has wider support across almost all programming languages and standard tools like jq. While YAML is easier for humans to read, it is more complex to parse and has several different versions that can lead to compatibility issues.

Download Deska for Your Workflow

Improving your CLI design is only half the battle. You also need an environment that can handle the complexity of modern development. Deska offers a flexible, local-first workspace that brings your terminals, editors, and AI agents together in one place.

You can download Deska for Mac, Windows, or Linux to start building your ideal developer setup. Whether you are debugging structured output or orchestrating multiple AI agents, the infinite canvas provides the space you need to work effectively. Download Deska here.

Machine-Readable CLI Output: JSON Flags Done Right | Deska