JSON vs Object Literals
JSON (JavaScript Object Notation) is a text format for exchanging data, used by web APIs, configuration files and local storage. Its syntax was taken from JavaScript object literals, so the two look alike, but they are different things: an object literal is code that creates a live object in a running program, while JSON is a string of characters that any language can read and write.
The differences in syntax
Section titled “The differences in syntax”An object literal allows things that JSON does not:
const recipe = { title: 'Pancakes', 'serves': 4, ingredients: ['flour', 'eggs', 'milk',], notes: undefined, cook() { return 'Cooking'; },};The same data written as JSON follows stricter rules:
{ "title": "Pancakes", "serves": 4, "ingredients": ["flour", "eggs", "milk"]}| Object literal | JSON | |
|---|---|---|
| Property names | quotes optional, single or double | double quotes required |
| Strings | single quotes, double quotes or backticks | double quotes only |
| Values | anything, including functions, undefined and dates |
strings, numbers, true, false, null, arrays and objects |
| Trailing commas | allowed | not allowed |
| Comments | allowed | not allowed |
From object to JSON with JSON.stringify()
Section titled “From object to JSON with JSON.stringify()”JSON.stringify() turns a value into a JSON string. Anything JSON cannot hold is dropped or converted on the way, so the result is not always a faithful copy:
const recipe = { title: 'Pancakes', serves: 4, vegetarian: true, ingredients: ['flour', 'eggs', 'milk'], notes: undefined, cook() { return 'Cooking'; }, updated: new Date(Date.UTC(2026, 11, 3)),};
const text = JSON.stringify(recipe);console.log(typeof text);console.log(text);This prints:
string{"title":"Pancakes","serves":4,"vegetarian":true,"ingredients":["flour","eggs","milk"],"updated":"2026-12-03T00:00:00.000Z"}The method cook and the property notes, whose value is undefined, have gone, and the date has become a string. A third argument adds indentation, which makes the output readable:
const recipe = { title: 'Pancakes', ingredients: ['flour', 'eggs'] };console.log(JSON.stringify(recipe, null, 2));This prints:
{ "title": "Pancakes", "ingredients": [ "flour", "eggs" ]}Stringify ( ) - Optional Parameters covers the second and third arguments.
From JSON to object with JSON.parse()
Section titled “From JSON to object with JSON.parse()”JSON.parse() reads a JSON string and builds the matching objects, arrays and values, which can then be used like any others:
const text = '{"title":"Pancakes","serves":4,"ingredients":["flour","eggs","milk"]}';const recipe = JSON.parse(text);
console.log(recipe.serves + 1);console.log(recipe.ingredients.length);This prints:
53A date that went through JSON.stringify() comes back as a string, so it has to be turned into a Date again with new Date(text) if it is needed as a date.
Invalid JSON throws
Section titled “Invalid JSON throws”Because JSON is strict, text that would be a valid object literal is often invalid JSON. JSON.parse() throws a SyntaxError for each of these: single quotes, an unquoted property name and a trailing comma:
const attempts = [ "{'title': 'Pancakes'}", '{title: "Pancakes"}', '{"serves": 4,}',];
for (const text of attempts) { try { JSON.parse(text); } catch (err) { console.log(err.name); }}This prints:
SyntaxErrorSyntaxErrorSyntaxErrorWrap JSON.parse() in try...catch whenever the text comes from outside the program, such as from a network response or from local storage.
Steve Griffith covers this topic in his video JSON vs JavaScript Object Literals.