Missing commas and brackets are the most common structural errors in JSON. These errors can be frustrating to find in large files. This guide shows you systematic ways to locate and fix them.
Understanding the Error
When you see errors like "Expected comma" or "Expected closing bracket", it means your JSON structure is broken:
SyntaxError: Expected ',' or '}' after property value
JSON.parse: expected ',' or ']' after array element
ValueError: Expecting ',' delimiter: line 5 column 3
Common Missing Comma Errors
1. Between Object Properties
// INVALID: missing comma between properties
{
"name": "John"
"age": 30
}
// VALID
{
"name": "John",
"age": 30
}
2. Between Array Elements
// INVALID: missing comma between elements
[
"apple"
"banana"
"cherry"
]
// VALID
[
"apple",
"banana",
"cherry"
]
3. After Nested Objects
// INVALID: missing comma after nested object
{
"user": {
"name": "John"
}
"settings": {
"theme": "dark"
}
}
// VALID
{
"user": {
"name": "John"
},
"settings": {
"theme": "dark"
}
}
Common Missing Bracket Errors
1. Unclosed Object
// INVALID: missing closing }
{
"name": "John",
"address": {
"city": "NYC"
}
// VALID: added closing }
{
"name": "John",
"address": {
"city": "NYC"
}
}
2. Unclosed Array
// INVALID: missing closing ]
{
"items": [
"one",
"two",
"three"
}
// VALID: added closing ]
{
"items": [
"one",
"two",
"three"
]
}
3. Mixed Bracket Types
// INVALID: wrong closing bracket type
{
"data": [
{"id": 1}
}
}
// VALID: correct bracket type
{
"data": [
{"id": 1}
]
}
Systematic Debugging Approach
The easiest way is to paste your JSON into our JSON formatter online. It will highlight the exact line and position of the error.
Method 1: Bracket Counting
function countBrackets(json) {
let openBraces = 0, closeBraces = 0;
let openBrackets = 0, closeBrackets = 0;
for (const char of json) {
if (char === '{') openBraces++;
if (char === '}') closeBraces++;
if (char === '[') openBrackets++;
if (char === ']') closeBrackets++;
}
console.log(`Braces: ${openBraces} { vs ${closeBraces} }`);
console.log(`Brackets: ${openBrackets} [ vs ${closeBrackets} ]`);
return openBraces === closeBraces && openBrackets === closeBrackets;
}
Method 2: Binary Search
function findErrorPosition(json) {
let start = 0;
let end = json.length;
while (start < end) {
const mid = Math.floor((start + end) / 2);
const partial = json.substring(0, mid);
// Count open brackets
const opens = (partial.match(/[{\[]/g) || []).length;
const closes = (partial.match(/[}\]]/g) || []).length;
if (opens >= closes) {
start = mid + 1;
} else {
end = mid;
}
}
return start;
}
Method 3: Line-by-Line Validation
import json
def find_json_error_line(json_string):
lines = json_string.split('
')
for i in range(len(lines), 0, -1):
partial = '
'.join(lines[:i])
# Try to complete the JSON
test = partial + '}' * 10 + ']' * 10
try:
json.loads(test)
return None # Valid
except json.JSONDecodeError as e:
if e.lineno <= i:
return e.lineno, e.colno, e.msg
return 1, 1, "Invalid JSON"
Using Code Editors
VS Code
1. Open JSON file
2. Press Ctrl+Shift+P (Cmd+Shift+P on Mac)
3. Type "Format Document"
4. Errors will be underlined in red
Online Validators
Use our JSON formatter online to instantly validate and format your JSON. It shows errors with exact line numbers and suggestions.
Prevention Tips
- Use a JSON-aware editor with syntax highlighting
- Format JSON after every edit
- Use programmatic JSON generation instead of manual editing
- Enable auto-formatting on save in your editor
# Always use json library to generate JSON
import json
data = {
"name": "John",
"items": ["a", "b", "c"]
}
# This will never have missing commas or brackets
json_string = json.dumps(data, indent=2)
Key Takeaways
- Missing commas usually occur between properties or array elements
- Missing brackets often happen with nested structures
- Use bracket counting to identify imbalances
- A good JSON formatter is your best debugging tool
Paste your JSON into our JSON formatter online to instantly find and fix structural errors.