πŸ”’ JSON Data Types
Estimated reading: 3 minutes 34 views

🧡 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 quotesStrings are always enclosed in " (not ')
Can contain Unicode charactersYou can use UTF-8 or escaped Unicode codes like \u00A9
Can include escape charactersSuch as \n (newline), \t (tab), \\ (backslash), \" (double quote)
Cannot contain unescaped control charsMust 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

EscapeMeaning
\"Double quote
\\Backslash
\/Forward slash
\bBackspace
\fForm feed
\nNewline
\rCarriage return
\tTab
\uUnicode 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Jason String

Or Copy Link

CONTENTS
Scroll to Top