π§΅ JSON String β Rules, Format, and Examples (2025 Guide)
π§² Introduction β What is a JSON String?
In JSON, a string is one of the most important data types used to represent textual data such as names, messages, URLs, file paths, and more. JSON strings are always written in double quotes and support a variety of special characters using escape sequences.
π― In this guide, youβll learn:
- How to define and format strings in JSON
- Special characters supported in JSON strings
- Valid and invalid string examples
- How to work with strings in JavaScript
β JSON String β Syntax Rules
πΉ Rule | π Description |
---|---|
Must use double quotes | Strings are always enclosed in " (not ' ) |
Can contain Unicode characters | You can use UTF-8 or escaped Unicode codes like \u00A9 |
Can include escape characters | Such as \n (newline), \t (tab), \\ (backslash), \" (double quote) |
Cannot contain unescaped control chars | Must escape characters like backslash and double quotes |
π§ͺ Example β Simple JSON Strings
{
"name": "Alice",
"city": "New York",
"language": "JavaScript"
}
π Explanation:
"name": "Alice"
β A basic string value"city": "New York"
β Includes a space within the string"language": "JavaScript"
β Another typical string usage
π§ͺ Example β JSON Strings with Escape Characters
{
"message": "He said, \"Hello!\"",
"path": "C:\\Users\\Alice\\Documents",
"multiline": "Line1\nLine2"
}
π Explanation:
\"
β Escapes a double quote\\
β Escapes a backslash\n
β Newline character
π List of JSON Escape Characters
Escape | Meaning |
---|---|
\" | Double quote |
\\ | Backslash |
\/ | Forward slash |
\b | Backspace |
\f | Form feed |
\n | Newline |
\r | Carriage return |
\t | Tab |
\u | Unicode character |
π« Invalid JSON Strings
{
'name': 'John' // β Invalid β single quotes
}
{
"quote": "He said, "Hi"" // β Invalid β unescaped internal quotes
}
β Fix:
{
"name": "John",
"quote": "He said, \"Hi\""
}
βοΈ JSON Strings in JavaScript
const json = '{ "greeting": "Hello World!" }';
const obj = JSON.parse(json);
console.log(obj.greeting); // Output: Hello World!
π Explanation:
JSON.parse()
converts a JSON string to a JavaScript object- The value of
"greeting"
becomes a native string in JavaScript
π Summary β Recap & Next Steps
JSON strings are versatile and essential for exchanging text-based information. They must follow specific rules, especially regarding quotes and special characters.
π Key Takeaways:
- JSON strings use double quotes only
- Escape special characters like quotes and backslashes
- Strings can hold letters, numbers, symbols, spaces, and Unicode
- Avoid single quotes, unescaped control characters, and line breaks
βοΈ Real-world use:
Used in API responses, form submissions, URLs, error messages, and UI content in JSON-based systems.
β FAQ β JSON String
β Can I use single quotes for strings in JSON?
β No. JSON strictly requires double quotes for all string values and keys.
β How do I include quotes inside a JSON string?
β
Use the escape character \"
.
Example: "She said, \"Hello!\""
β Are newline characters allowed in strings?
β
Yes, using \n
.
Example: "Line1\nLine2"
β What is the difference between a string and a number in JSON?
β
Strings are enclosed in quotes; numbers are not.
Example:
{ "price": "100" } // string
{ "price": 100 } // number
Share Now :