Skip to main content

How to Write a String in JSON

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Text in JSON is a string: characters between double quotes, such as "Ada". Two quotes with nothing between them make an empty string, and digits in quotes stay text, which keeps a code such as "042" exactly as written.

Syntax​

General form
"<characters>"
  • " and ": a string starts and ends with a double quotation mark. Single quotes aren't allowed.
  • <characters>: any text. Nothing at all gives the empty string, "".

The JSON standard, RFC 8259, says: A string begins and ends with quotation marks. All Unicode characters may be placed within the quotation marks, except for the characters that MUST be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F). So a double quote or a backslash inside the text is written with a backslash in front of it, as \" or \\.

The Example​

A file called strings.json has three strings in an object: a name, Ada; a nickname that's empty; and a code, 042, that has to keep its leading zero. jq, a command-line JSON tool, checks that it's valid.

Prerequisites​

  • jq, to check the file. Run jq --version to see which one you have; this guide uses jq 1.8.2 (Download jq).

Step 1: Write the Strings​

Create a file called strings.json with this text:

strings.json, a JSON object with three string values: name Ada, nickname as an empty string, and code 042 in quotes

strings.json
{
"name": "Ada",
"nickname": "",
"code": "042"
}

The names on the left are strings too: every name in an object is written in double quotes.

Step 2: Check It with jq​

In a terminal, in the folder with the file, run:

Terminal
jq . strings.json

jq's manual explains the .: the . program's main use is to validate and pretty-print the inputs. The file was valid, and jq printed all three strings back unchanged, including the empty one and the leading zero:

Output
{
"name": "Ada",
"nickname": "",
"code": "042"
}

Quotes, Backslashes and Line Breaks Inside a String​

A double quote would end the string early, so inside a string it's written as \". A backslash is written as \\, and a line break as \n. escapes.json uses all three:

escapes.json
{
"quote": "She said \"hi\"",
"path": "C:\\temp",
"lines": "one\ntwo"
}
Terminal
jq . escapes.json

jq accepted the file and printed it back with the same escapes:

Output
{
"quote": "She said \"hi\"",
"path": "C:\\temp",
"lines": "one\ntwo"
}

Common Mistakes​

I left the quotes off Ada in a copy of the file, { "name": Ada } laid out over three lines, and ran jq . on it. jq 1.8.2 said:

Output
jq: parse error: Invalid numeric literal at line 3, column 0

The message talks about a number because jq tried to read the bare word as one. Text without quotes is the usual cause.

MistakeWhat happensDo this instead
Text without quotesInvalid numeric literalPut it in double quotes
Single quotes, 'Ada'Invalid string literal; expected ", but got 'Use double quotes
A code such as 042 without quotesjq read it as the number 42, although the standard doesn't allow a leading zeroWrite it as a string, "042"

Troubleshooting​

SymptomLikely causeFix
Invalid numeric literalA word without quotesAdd double quotes
A code loses its leading zeroIt was written as a numberPut it in quotes
Invalid numeric literal in the middle of a stringA double quote inside the text isn't escaped, as in "She said "hi""Write it as \"
Invalid string: control characters from U+0000 through U+001F must be escapedA real line break inside the quotesWrite it as \n

FAQ​

Is an empty string the same as no value?​

No. "" is a string with no characters. JSON has a separate literal, null, for no value, and leaving the pair out is a third option.

Is "3" the same as 3?​

No. "3" is text and 3 is a number. jq prints "3" with its quotes, which is how you can tell them apart in its output.

Can a string span several lines?​

Not with real line breaks inside the quotes: line breaks are control characters, which the standard says MUST be escaped. jq stopped a file with one with Invalid string: control characters from U+0000 through U+001F must be escaped. A line break inside a string is written as \n.

Conclusion​

A JSON string is text in double quotes: "Ada", the empty string "", or digits that must stay text, such as "042". jq . strings.json printed all three back unchanged, and text without quotes gave Invalid numeric literal.

Additional Resources​