Skip to main content

How to Write JSON on One Line Without Spaces

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

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​

General form
{"<name>":<value>,"<name>":<value>}
PartWhat 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 --version prints the version you have; this guide uses jq 1.8.2 (Download jq).

Step 1: Write the Indented Version​

person.json
{
"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​

person-compact.json
{"name":"Ada","address":{"city":"New York","zip":"10001"},"langs":["en","fr"]}

macOS-style window titled person-compact.json showing the whole person object on one line, with no spaces after the colons or commas: name Ada, an address with city New York and zip 10001, and langs en and 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​

Terminal
jq --slurp '.[0] == .[1]' person.json person-compact.json
Output
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​

Terminal
jq -c . person.json
Output
{"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​

Terminal
wc -c person.json person-compact.json
Output
120 person.json
79 person-compact.json
199 total

macOS-style terminal window titled Terminal showing a real zsh session: cat prints the one-line file, jq spreads it out over eleven lines, jq -c turns person.json into the same one line, jq --slurp prints true, and wc -c shows 120 and 79 bytes.

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​

MistakeWhat happensDo this instead
Deleting every space with tr -d ' \n' < person.jsonThe space inside the string went too: the city came out as NewYorkUse 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 15Delete the last comma
Leaving out the dot: jq -c person.jsonjq: error: person/0 is not defined at <top-level>, line 1, column 1: and jq: 1 compile errorWrite jq -c . person.json

Here's the whole message from the last row:

Output
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:

Terminal
jq . person-compact.json
Output
{
"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​