Parse Dosya CLI JSON in a script
Preserve the Dosya CLI exit status, parse valid command-specific JSON even for partial failures, and gate dependent work on a verified success.
Last reviewed
A robust shell wrapper preserves the Dosya process status before running jq. JSON output is command-specific, and batch commands normally emit usable success and failure receipts before exiting 1 when any member failed.

Before you start
- Use temporary files with restrictive permissions for outputs that may contain names or IDs.
- Keep
set -o pipefailwhen using a pipeline, or avoid a pipeline so the parser cannot hide the CLI status. - Choose a precise
jq -eassertion for the command’s documented response.
Steps
- Create separate stdout and stderr files, then run the command with
--json; for example,dosya whoami --json >result.json 2>result.err. - Capture
status=$?immediately and classify it without replacing it withjq’s status. Keep stderr beside that original numeric result. - When stdout is nonempty, run
jq -e . result.jsoneven after a nonzero exit. Preserve valid command output; if parsing fails, report that separately without losing the Dosya status. - Apply a command-specific selector, such as the required user identifier from
whoamior the expected item collection fromls. There is no universal response envelope. - For
mv,rm, recursive download, trash, star, or sync batches, inspect the command’sfailuresentries and successful counts. Partial work normally produces valid JSON and exits 1. - Start dependent work only when the Dosya status is 0 and the command-specific postcondition passes. Retain a sanitized partial receipt for recovery, then remove sensitive temporary files.

Expected result
The wrapper preserves the CLI failure class, extracts useful receipts from valid nonzero output, and starts dependent work only after exit 0 and the command-specific postcondition both pass.
Troubleshooting
The JSON file is empty. Read stderr and the saved process status; do not let jq become the reported failure.
A batch exits 1 with valid JSON. Preserve successful counts and failure entries, repair or report each failed member, and keep the overall task unsuccessful.
Warnings break parsing. They should be on stderr. Remove stream merging and confirm the wrapper invokes the current CLI binary.
Related guides
Was this helpful?
Loading helpfulness results…
Voting saves a necessary ballot cookie for up to 180 days so you can change your answer.
Questions and replies
Ask about the steps in this guide. Questions are reviewed before publication.
Keep account details, credentials and private files out of public questions. Use private support for those.
Loading questions…
Sign in to your Dosya account to ask a question.