JSON vs JavaScript Objects: Key Differences Explained
Understanding the crucial differences between JSON and JavaScript object literals
Last updated: January 21, 2026 • 9 min read
The Confusion
Many developers confuse JSON (JavaScript Object Notation) with JavaScript objects because they look similar. However, they are fundamentally different. JSON is a text format for data exchange, while JavaScript objects are data structures in memory.
Side-by-Side Comparison
| Feature | JSON | JavaScript Object |
|---|---|---|
| Type | Text format (string) | In-memory data structure |
| Keys | Must be double-quoted strings | Can be unquoted identifiers |
| String Values | Double quotes only | Single or double quotes |
| Functions | ❌ Not allowed | ✅ Allowed |
| Comments | ❌ Not allowed | ✅ Allowed |
| Trailing Commas | ❌ Not allowed | ✅ Allowed (ES5+) |
| undefined | ❌ Not supported | ✅ Supported |
| Date Objects | ❌ Must use strings | ✅ Native Date objects |
| NaN/Infinity | ❌ Not supported | ✅ Supported |
Key Difference #1: Keys Must Be Quoted in JSON
JavaScript Object (Valid):
const user = {
name: "John", // Unquoted key
age: 30,
'city': "NYC" // Can use quotes, but optional
};JSON (Must Have Quoted Keys):
{
"name": "John", // Keys MUST be quoted
"age": 30,
"city": "NYC"
}Key Difference #2: String Quotes
JavaScript Object:
const user = {
name: 'John', // Single quotes OK
city: "New York" // Double quotes OK
};JSON:
{
"name": "John", // ONLY double quotes
"city": "New York"
}Key Difference #3: Functions Are Not Allowed in JSON
JavaScript Object (Valid):
const user = {
name: "John",
greet: function() {
return "Hello!";
},
// Arrow function
getAge: () => 30
};JSON (Invalid - Functions Not Supported):
{
"name": "John",
"greet": function() { // ❌ ERROR: Not valid JSON
return "Hello!";
}
}Key Difference #4: Special Values
JavaScript Object:
const data = {
value: undefined, // ✅ Valid in JS
result: NaN, // ✅ Valid in JS
infinity: Infinity, // ✅ Valid in JS
date: new Date() // ✅ Valid in JS
};JSON (Must Use null or strings):
{
"value": null, // Use null instead of undefined
"result": null, // NaN not supported
"infinity": null, // Infinity not supported
"date": "2026-01-21T10:30:00Z" // Dates must be strings
}Converting Between JSON and JavaScript Objects
JavaScript Object → JSON String (Serialization)
const user = {
name: "John",
age: 30,
city: "NYC"
};
// Convert to JSON string
const jsonString = JSON.stringify(user);
console.log(jsonString);
// Output: '{"name":"John","age":30,"city":"NYC"}'
// With formatting (pretty print)
const formatted = JSON.stringify(user, null, 2);
console.log(formatted);
/* Output:
{
"name": "John",
"age": 30,
"city": "NYC"
}
*/JSON String → JavaScript Object (Parsing)
const jsonString = '{"name":"John","age":30,"city":"NYC"}';
// Convert to JavaScript object
const user = JSON.parse(jsonString);
console.log(user.name); // Output: "John"
console.log(user.age); // Output: 30Common Pitfalls When Converting
1. Functions Are Lost
const user = {
name: "John",
greet: function() { return "Hello"; }
};
const json = JSON.stringify(user);
console.log(json);
// Output: '{"name":"John"}' // greet function is lost!2. undefined Values Are Removed
const user = {
name: "John",
middleName: undefined,
age: 30
};
const json = JSON.stringify(user);
console.log(json);
// Output: '{"name":"John","age":30}' // middleName is removed!3. Dates Become Strings
const data = {
timestamp: new Date()
};
const json = JSON.stringify(data);
console.log(json);
// Output: '{"timestamp":"2026-01-21T10:30:00.000Z"}'
const parsed = JSON.parse(json);
console.log(typeof parsed.timestamp); // "string", not Date object!
// Need to manually convert back
parsed.timestamp = new Date(parsed.timestamp);Advanced JSON.stringify() Options
Replacer Function
const user = {
name: "John",
password: "secret123",
age: 30
};
// Exclude password from JSON
const json = JSON.stringify(user, (key, value) => {
if (key === "password") return undefined;
return value;
});
console.log(json);
// Output: '{"name":"John","age":30}'Selective Properties
const user = {
name: "John",
age: 30,
password: "secret",
email: "john@example.com"
};
// Only include specific keys
const json = JSON.stringify(user, ["name", "email"]);
console.log(json);
// Output: '{"name":"John","email":"john@example.com"}'When to Use Each
Use JSON When:
- Exchanging data between client and server (APIs)
- Storing data in files or databases
- Transmitting data over a network
- Creating configuration files
- Need language-independent data format
Use JavaScript Objects When:
- Working with data in your JavaScript application
- Need methods/functions attached to data
- Want to use special values (undefined, NaN, etc.)
- Need Date objects or other complex types
- Working with data that won't be serialized
Best Practices
- Validate JSON: Always validate JSON from external sources using our JSON validator
- Handle Dates Carefully: Convert date strings back to Date objects after parsing
- Use try-catch: Wrap JSON.parse() in try-catch to handle invalid JSON
- Be Aware of Data Loss: Functions and undefined values are lost in serialization
- Consider Alternatives: For complex objects with methods, consider using classes with toJSON()
Error Handling Example
function safeJsonParse(jsonString) {
try {
return JSON.parse(jsonString);
} catch (error) {
console.error("Invalid JSON:", error.message);
return null;
}
}
const result = safeJsonParse('{"name": "John"}'); // ✅ Success
const invalid = safeJsonParse('{name: "John"}'); // ❌ Returns null, logs errorTools for Working with JSON and JavaScript
Conclusion
While JSON and JavaScript objects look similar, they serve different purposes. JSON is a text-based data format for exchanging information, while JavaScript objects are in-memory data structures with more flexibility. Understanding these differences helps you avoid common mistakes when working with data in JavaScript applications.
Work with JSON Confidently
Use our free tools to validate, format, and work with JSON data!
Validate JSON Now →