paritytech / paritytech/polkadot-cli
feat: add --output ndjson for pipe-friendly streaming output
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 10
- Forks
- 2
- Avg merge
- 12h 35m
- Merged PRs (30d)
- 4
Description
Problem
JSON output (--output json) emits a single JSON array, which is unsuitable for streaming/piped contexts where line-by-line processing is preferred. Tools like jq, grep, and awk work best with one JSON object per line (NDJSON).
Related
- #64 (structured JSON output)
- #63
Proposed Solution
Add ndjson as a new output format option: --output ndjson
Implementation
Add a formatNdjson() function to src/core/output.ts:
function formatNdjson(data: unknown): string {
if (Array.isArray(data)) {
// One compact JSON line per array element
return data.map(item => JSON.stringify(item)).join('\n')
}
// Non-arrays emit a single line
return JSON.stringify(data)
}
Update printResult to handle the new format:
case 'ndjson':
console.log(formatNdjson(result))
break
Use cases
# Process each storage entry individually
polkadot query Staking.Validators --output ndjson | jq '.commission'
# Stream live updates as individual lines (with --watch)
polkadot query System.Events --watch --output ndjson | while read line; do
echo "$line" | jq -r '.event'
done
# Count entries with grep
polkadot query Staking.Validators --output ndjson | wc -l
Priority
MEDIUM
Files
src/core/output.tssrc/core/output.test.tssrc/cli.ts(addndjsonto output format choices)
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/core/output.ts and review printResult plus the existing output formatters. Add the ndjson format there, update the output format choices in src/cli.ts, and extend src/core/output.test.ts for arrays and non-array values. Done means --output ndjson emits one compact JSON object per line and supports the documented piping use cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- cli
- Issue type
- Feature
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100