How to Write JSON on One Line Without Spaces
JSON doesn't need its line breaks and indents. An object written on one line, with no spaces between its parts, holds exactly the same data as the same object spread over several indented lines, in fewer bytes.
This guide writes a small object both ways, checks with jq 1.8.2 that they're equal, and has jq write the one-line form for you.
Syntax
{"<name>":<value>,"<name>":<value>}
| Part | What it means |
|---|---|
{ and } | The start and end of the object |
"<name>" | A name, in double quotes |
: | Separates the name from its value |
<value> | The value: a string in double quotes, a number, true, false, null, another object or an array |
, | Separates one name and value from the next |
There's no space or line break anywhere between the parts. The JSON standard, RFC 8259, describes an object like this: A single colon comes after each name, separating the name from the value. A single comma separates a value from a following name. Arrays work the same way, with square brackets: ["en","fr"].
On spaces, the RFC says: Insignificant whitespace is allowed before or after any of the six structural characters. The six are [, {, ], }, : and ,. Whitespace there is allowed, not required, so leaving it all out doesn't change what the JSON means.
The Example
person.json describes Ada: her name, an address with a city and a zip code, and two languages, written over eleven indented lines. You write the same data on one line in person-compact.json.
Prerequisites
- jq, to check the files.
jq --versionprints the version you have; this guide uses jq 1.8.2 (Download jq).
Step 1: Write the Indented Version
{
"name": "Ada",
"address": {
"city": "New York",
"zip": "10001"
},
"langs": [
"en",
"fr"
]
}
Every line break, indent and space outside the strings in this file sits next to one of the six structural characters, so all of it is insignificant whitespace.
Step 2: Write It on One Line
{"name":"Ada","address":{"city":"New York","zip":"10001"},"langs":["en","fr"]}

There are no spaces after the colons or commas, and the whole object sits on one line. The space in New York stays: it's inside a string, so it's part of the value, not whitespace between the parts.
Step 3: Check That Both Hold the Same Data
jq --slurp '.[0] == .[1]' person.json person-compact.json
true
--slurp reads both files into one list. The manual: Instead of running the filter for each JSON object in the input, read the entire input stream into a large array and run the filter just once. .[0] is the first file, .[1] the second, and == compares them. jq printed true: the two files hold the same data.
Step 4: Let jq Write the One Line
jq -c . person.json
{"name":"Ada","address":{"city":"New York","zip":"10001"},"langs":["en","fr"]}
-c is short for compact. The manual: By default, jq pretty-prints JSON output. Using this option will result in more compact output by instead putting each JSON object on a single line. jq's line is exactly what cat person-compact.json printed for the file written by hand.
Step 5: Compare the Sizes
wc -c person.json person-compact.json
120 person.json
79 person-compact.json
199 total

wc -c counts the bytes in each file. The one-line file is 41 bytes smaller, about a third of the indented one, and every one of those bytes was a space or a line break.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Deleting every space with tr -d ' \n' < person.json | The space inside the string went too: the city came out as NewYork | Use jq -c ., which keeps the spaces inside strings |
A comma left after the last pair: {"name":"Ada",} | jq: parse error: Expected another key-value pair at line 1, column 15 | Delete the last comma |
Leaving out the dot: jq -c person.json | jq: error: person/0 is not defined at <top-level>, line 1, column 1: and jq: 1 compile error | Write jq -c . person.json |
Here's the whole message from the last row:
jq: error: person/0 is not defined at <top-level>, line 1, column 1:
person.json
^^^^^^
jq: 1 compile error
Without the dot, jq read person.json as the program to run, not as a file to read, and stopped at the word person, which the carets mark. The manual shows the same failure with another word: jq foo, which will generally fail because foo is not defined.
FAQ
How do I make one-line JSON readable again?
Run it through jq's . filter, which spreads it out with an indent of two spaces:
jq . person-compact.json
{
"name": "Ada",
"address": {
"city": "New York",
"zip": "10001"
},
"langs": [
"en",
"fr"
]
}
The manual: the . program's main use is to validate and pretty-print the inputs.
Conclusion
{"name":"Ada","address":{"city":"New York","zip":"10001"},"langs":["en","fr"]} holds the same data as the eleven-line person.json: jq compared the two and printed true, and jq -c . wrote that same line from the indented file, 41 bytes smaller. Leave the spaces inside strings alone, and let jq -c do the work.
Additional Resources
- RFC 8259, section 2: JSON Grammar: the six structural characters and where whitespace is allowed.
- jq 1.8 manual:
-c,--slurpand the.filter.
