Blogchevron_rightJSON Tools
JSON Tools

Convert JSON to One Line in Terminal: jq, Python, and Node.js

Command-line JSON conversion is essential for scripting and automation. Here are the fastest ways to compact JSON in the terminal using jq, Python, and Node.js.

April 18, 2026·6 min read

Using jq to Compact JSON

jq is the most powerful command-line JSON tool. The -c (compact) flag outputs JSON on a single line: jq -c . input.json. For a string from a variable: echo "$MY_JSON" | jq -c .. For API responses: curl -s https://api.example.com/data | jq -c .

jq also combines compaction with transformation. jq -c "{id, name}" input.json outputs a compact JSON with only the id and name fields. jq -c ".items[]" input.json outputs each array element as a separate compact line, which is JSONL format.

Using Python to Compact JSON

Python's json module provides a reliable one-liner: python3 -c "import sys,json; print(json.dumps(json.load(sys.stdin)))" < input.json. This reads from stdin, validates the JSON, and outputs a compact single-line version. The validation step ensures the output is always valid JSON.

For files: python3 -c "import json; print(json.dumps(json.load(open('input.json'))))" or python3 -m json.tool --no-indent input.json (Python 3.9+). The --no-indent flag produces compact output; the default json.tool produces formatted output.

Using Node.js to Compact JSON

Node.js: node -e "const fs=require('fs'); console.log(JSON.stringify(JSON.parse(fs.readFileSync(process.argv[1],'utf8'))))" input.json. More concisely via pipe: cat input.json | node -e "let d=''; process.stdin.on('data',c=>d+=c).on('end',()=>console.log(JSON.stringify(JSON.parse(d))))".

In a Node.js script or npm script, compacting JSON from a file is straightforward: const compact = JSON.stringify(JSON.parse(fs.readFileSync("input.json", "utf8"))); fs.writeFileSync("output.json", compact). This validates and compacts in one step.

Chaining Commands for API Workflows

curl -s https://api.example.com/config | jq -c . > compact-config.json fetches a JSON API response and saves the compact version locally. curl -s url | jq -c ".settings" > settings.json extracts and compacts a specific field.

In shell scripts, storing compact JSON in a variable: COMPACT=$(cat config.json | jq -c .) and then using it: curl -X POST -d "$COMPACT" -H "Content-Type: application/json" https://api.example.com/update. This pattern is common in deployment scripts.

Try JSON to One Line Free Online

No sign-up required. 100% client-side — your data never leaves your browser.

Open JSON to One Linearrow_forward

Frequently Asked Questions

What is the difference between jq -c and jq -r?

-c (compact) outputs JSON on one line. -r (raw) outputs string values without JSON quotes — useful for extracting string values for use in shell scripts. Use -c for JSON output and -r for plain text.

How do I validate JSON before compacting it?

Both jq and Python's json.dumps/json.load validate JSON during parsing. If the input is invalid, they print an error and exit with a non-zero status code. This makes them safe for use in pipelines.

Can I use sed or awk to remove JSON newlines?

Technically yes, but this is fragile — sed and awk treat JSON as text and can corrupt it if newlines appear inside string values. Always use a JSON-aware tool like jq or Python.

Convert JSON to One Line in Terminal: jq, Python, and