# How to script the DunSocial CLI with JSON output > Add --json (or set DUN_JSON=1) to get structured output with ok, data & error fields on stdout, while progress & debug lines go to stderr. In CI or a non-TTY the CLI defaults to JSON, so scripts can parse stdout safely. Updated: 2026-10-05. ## Why scripts need JSON Human output is readable but changes shape between versions & commands. Structured output stays stable, so a script can pull out an id or a status without scraping text. ## Turn it on Three ways, from narrowest to widest: ```bash dun accounts list --json # one command export DUN_JSON=1 # whole shell session # CI=1 or a non-TTY stdout # defaults to JSON automatically ``` Setting `DUN_JSON=0` forces human output even when it would otherwise default to JSON. ## The output shape A success looks like this: ```json { "ok": true, "data": { } } ``` A failure looks like this: ```json { "ok": false, "error": { "code": "validation", "message": "Missing --text", "details": null } } ``` Check `ok` first, then read `data` or `error.code`. ## stdout is for data only Progress lines & debug output go to stderr. That means a script can capture stdout & trust that it's pure JSON, even when `--debug` or a progress message is also running. ```bash dun accounts list --platform x --json 2>/dev/null ``` Redirecting stderr, as above, keeps the terminal quiet without affecting what you parse. DunSocial's CLI keeps that split consistent across commands, so the same redirect works everywhere. ## Pulling out a value Combine with a JSON tool such as `jq` to read a field: ```bash ASSET_ID=$(dun media upload ./shot.png --json | jq -r '.data.id') ``` The exact field path depends on the command, so look at the real JSON once with `--json` before writing the extraction. DunSocial's upload output includes the asset id, so you can chain an upload & a schedule in one script. ## Handy global flags | Flag | Purpose | |------|---------| | `--json` / `-j` | Machine-readable output | | `--quiet` / `-q` | Suppress human success output | | `--yes` / `-y` | Skip destructive confirmations | | `--debug` | Log HTTP method, path & status to stderr | | `--workspace` / `-w` | Override the workspace for one command | DunSocial's global flags work on every command, so you only learn them once. ## A reliable pattern Fetch ids at the start of a script, validate, then schedule, & stop on any failure with `set -e`. Pair that with the exit codes in the next article so the script can react to auth, validation & rate-limit errors differently. DunSocial's CLI is built for exactly this style of use, so everything it prints for humans has a JSON equivalent. ## Related guides - [What do the DunSocial CLI exit codes mean?](https://www.dunsocial.com/hub/dunsocial-cli-guides/what-do-the-dunsocial-cli-exit-codes-mean.md) - [How to run the DunSocial CLI in GitHub Actions](https://www.dunsocial.com/hub/dunsocial-cli-guides/how-to-run-the-dunsocial-cli-in-github-actions.md) - [How to find the right social account id in the DunSocial CLI](https://www.dunsocial.com/hub/dunsocial-cli-guides/how-to-find-the-right-social-account-id-in-the-dunsocial-cli.md) - [How to bulk schedule social media posts](https://www.dunsocial.com/hub/social-media-scheduler/how-to-bulk-schedule-social-media-posts.md) ## About this document This is the Markdown representation of https://www.dunsocial.com/hub/dunsocial-cli-guides/how-to-script-the-dunsocial-cli-with-json-output. The HTML version of the same page is at the same URL. You can also request Markdown from any page by sending `Accept: text/markdown`. Machine-readable summary of the whole site: https://www.dunsocial.com/llms.txt ## Company - Product: DunSocial, https://www.dunsocial.com - Legal entity: THISUX PRIVATE LIMITED, Chennai, Tamil Nadu, India - Support: support@dunsocial.com - Open the app: https://app.dunsocial.com/login