JSON Strings and Escaping - potatoscript/json GitHub Wiki
π― JSON Strings and Escaping: The Secret Language of Text! β¨
Welcome to the exciting journey of JSON strings and escaping characters! π§ββοΈπ
Imagine talking to a computer where you need to be very precise with your words.
If you forget the special rules, your message gets lost! π±
Donβt worry, though! By the end of this adventure, youβll be a JSON string master! π©
π What is a JSON String?
A string in JSON is like a sentence that holds text. Itβs wrapped between double quotes (" "
), like a present! π
π― Basic Example: JSON String
{
"name": "Lucy",
"message": "Welcome to JSON world!"
}
π Explanation:
name
is the key, and"Lucy"
is the string value.message
is the key, and"Welcome to JSON world!"
is another string value.
π¨ Important Rules for JSON Strings
- β
JSON strings must be enclosed in double quotes (
" "
), not single quotes. - β You cannot leave a string without quotes.
- β
Special characters inside a string need to be escaped using a backslash (
\
).
π What is Escaping in JSON?
Escaping is like putting on armor for special characters that have a different meaning in JSON. π‘οΈβ¨
When you want to include a special character in a JSON string, you use a backslash (\
) to tell JSON:
βHey, treat this character as part of the string, not as a command!β
π― Example: Escaped String
{
"message": "Lucy said, \"JSON is awesome!\""
}
π Explanation:
\"
tells JSON to ignore the meaning of the quote and include it in the string.- The final value of
message
will be:
Lucy said, "JSON is awesome!"
π Common Escape Characters in JSON
Escape Sequence | Meaning | Example | Result |
---|---|---|---|
\" |
Double quote | "Hello \"World\"" |
Hello "World" |
\\ |
Backslash | "C:\\Users\\Lucy" |
C:\Users\Lucy |
\/ |
Forward slash | "https:\/\/example.com" |
https://example.com |
\b |
Backspace | "Hello\bWorld" |
HelloWorld |
\f |
Form feed | "Hello\fWorld" |
HelloWorld |
\n |
New line | "Hello\nWorld" |
``` |
Hello
World
``` |
| \r
| Carriage return | "Hello\rWorld"
| World
|
| \t
| Tab | "Hello\tWorld"
| Hello World
|
| \uXXXX
| Unicode character | "\u2605"
| β
|
π Story Time: Lucy and the Secret Codes!
Lucy is sending a message to her friend about her favorite book. π
{
"message": "Lucy said, \"I love reading 'Harry Potter' at night!\""
}
π Explanation:
\"
protects the quotes around"Harry Potter"
.- Lucyβs message is safely delivered! π
πΉοΈ Special Case: Escaping Backslashes
To include a backslash (\
) in your string, escape it with another backslash! π₯
Think of it as giving your backslash a twin to protect it. π―
π― Example: Backslash in JSON
{
"path": "C:\\Users\\Lucy\\Documents"
}
π Explanation:
\\
becomes a single backslash in the final string.- The final result:
C:\Users\Lucy\Documents
β‘ Multiline Strings in JSON
JSON does not support multiline strings directly.
If you need to break lines, use \n
to add a new line. β¨
π― Example: Multiline String with Escape Characters
{
"poem": "Roses are red,\nViolets are blue,\nJSON is fun,\nAnd so are you!"
}
π Result:
Roses are red,
Violets are blue,
JSON is fun,
And so are you!
π₯ Using Unicode in JSON Strings
Unicode allows you to include special characters by using \u
followed by a 4-digit hexadecimal code. π‘β¨
Itβs like using secret codes to add fancy characters! π
π― Example: Unicode in JSON
{
"symbol": "\u2605",
"heart": "\u2764"
}
π Result:
\u2605
becomes β\u2764
becomes β€οΈ
π΅οΈ How to Test Your JSON Strings
To test if your JSON string is valid:
- β Use an online JSON validator like jsonlint.com.
- β
Or use
JSON.parse()
in JavaScript to check validity.
π§ Common Mistakes and How to Fix Them
Mistake | Error Message | Fix |
---|---|---|
Single quotes ' |
Unexpected token |
Use double quotes " " |
Missing quotes | SyntaxError: Unexpected token |
Wrap strings in quotes |
Forgetting to escape | SyntaxError: Unexpected token |
Use \" for quotes inside strings |
Using a backslash \ |
SyntaxError: Invalid escape |
Use double backslash \\ |
π Modifying JSON Strings in JavaScript
You can easily modify or add escape characters in strings using JavaScript.
Letβs update Lucyβs favorite book!
π― Example: Modifying JSON Strings in JavaScript
let jsonString = '{ "message": "Lucy said, \\"I love reading Harry Potter!\\"" }';
// Parse the string to a JSON object
let data = JSON.parse(jsonString);
// Modify the message
data.message = "Lucy said, \"I now love reading Sherlock Holmes!\"";
// Convert back to JSON string
let updatedString = JSON.stringify(data);
console.log(updatedString);
π Result:
{"message":"Lucy said, \"I now love reading Sherlock Holmes!\""}