JSON

Invalid Character in JSON String: How to Resolve It

📅 December 14, 2025 ⏱️ 2 min read 👁️ 8 views 🏷️ JSON

Invalid character errors in JSON occur when special characters are not properly escaped or when encoding issues corrupt your data. This guide covers all common causes and solutions.

Understanding JSON String Rules

JSON strings have specific rules about which characters are allowed and how special characters must be escaped:


JSON strings must:
- Be wrapped in double quotes (")
- Escape special characters with backslash (\)
- Use Unicode escapes for control characters

Characters That Must Be Escaped


// These characters MUST be escaped in JSON strings:
\"  - Double quote
\\  - Backslash
\/   - Forward slash (optional but recommended)
\b  - Backspace
\f  - Form feed
\n  - Newline
\r  - Carriage return
\t  - Tab
\uXXXX - Unicode character

Common Invalid Character Errors

1. Unescaped Quotes


// INVALID: unescaped quote inside string
{"message": "He said "hello""}

// VALID: escaped quotes
{"message": "He said \"hello\""}

2. Unescaped Backslash


// INVALID: unescaped backslash
{"path": "C:\Users\name"}

// VALID: escaped backslashes
{"path": "C:\\Users\\name"}

3. Raw Newlines


// INVALID: actual newline in string
{"text": "Line 1
Line 2"}

// VALID: escaped newline
{"text": "Line 1\nLine 2"}

4. Control Characters


// INVALID: tab character (ASCII 9)
{"text": "Hello	World"}  // Actual tab character

// VALID: escaped tab
{"text": "Hello\tWorld"}

5. Non-UTF-8 Characters


# INVALID: non-UTF-8 bytes
b'{"name": "Café"}'  # Latin-1 encoding

# VALID: proper UTF-8 or Unicode escape
'{"name": "Cafe"}'
'{"name": "Caf\u00e9"}'

How to Fix Invalid Characters

Use our JSON formatter online to paste your JSON and it will highlight exactly where invalid characters are located.

JavaScript Solution


// Properly escape a string for JSON
function escapeJsonString(str) {
  return str
    .replace(/\\/g, '\\\\')
    .replace(/"/g, '\\"')
    .replace(/\n/g, '\\n')
    .replace(/\r/g, '\\r')
    .replace(/\t/g, '\\t');
}

// Or simply use JSON.stringify
const obj = { message: 'He said "hello"' };
const json = JSON.stringify(obj);
// Result: {"message":"He said \"hello\""}

Python Solution


import json

# Let Python handle escaping
data = {"message": 'He said "hello"'}
json_str = json.dumps(data)
# Result: {"message": "He said \"hello\""}

# Handle encoding issues
with open('data.json', 'r', encoding='utf-8') as f:
    data = json.load(f)

Fixing Encoding Issues


# Convert to UTF-8
def fix_encoding(byte_string):
    # Try common encodings
    for encoding in ['utf-8', 'latin-1', 'cp1252']:
        try:
            return byte_string.decode(encoding)
        except UnicodeDecodeError:
            continue
    return byte_string.decode('utf-8', errors='ignore')

Prevention Best Practices

  • Always use JSON.stringify() or json.dumps() to create JSON
  • Ensure files are saved with UTF-8 encoding
  • Validate JSON before storing or transmitting
  • Use a proper JSON library instead of string concatenation

To quickly validate and fix your JSON, paste it into our JSON formatter online which will identify problematic characters.

🏷️ Tags:
json invalid character json escape json encoding special characters json

📚 Related Articles