Sitemap
Pooja Chapole

Pooja Chapole

JavaScript JSON — Complete Notes

3 min readNov 1, 2026

--

1. What is JSON?

JSON stands for JavaScript Object Notation.

It is a lightweight data format used to store and exchange data between applications, especially between frontend and backend.

For example, when a frontend requests user details from a backend, the backend commonly sends the data in JSON format.

const user = {
name: "Pooja",
age: 25,
isActive: true
};

JSON looks similar to a JavaScript object, but they are not the same.

2. JavaScript Object vs JSON

JavaScript object:

const user = {
name: "Pooja",
age: 25
};

JSON string:

{
"name": "Pooja",
"age": 25
}

Important differences:

    JavaScript Object                                          JSON
Keys can be unquoted or quoted Keys must use double quotes
Strings can use single or double quotes Strings must use double quotes
Can contain functions Cannot contain functions
Can contain undefined Does not support undefined
Can contain comments in surrounding JS codeComments are not allowed

JSON supports:

  • Strings
  • Numbers
  • Booleans
  • null
  • Arrays
  • Objects

3. JSON.stringify()

JSON.stringify() converts a JavaScript value into a JSON string.

const user = {
name: "Pooja",
age: 25
};
const result = JSON.stringify(user);
console.log(result);

Output:

{"name":"Pooja","age":25}

You can also provide indentation to make the JSON easier to read.

console.log(JSON.stringify(user, null, 2));

Output:

{
"name": "Pooja",
"age": 25
}

Remember: JSON.stringify() returns a string, not a JavaScript object.

4. JSON.parse()

JSON.parse() converts a valid JSON string into a JavaScript value.

const jsonData = '{"name":"Pooja","age":25}';
const user = JSON.parse(jsonData);
console.log(user.name);
console.log(user.age);

Output:

Pooja
25

Remember:

  • JSON.stringify() → JavaScript value to JSON string.
  • JSON.parse() → JSON string to JavaScript value.

5. Nested Objects and Arrays

JSON supports nested objects and arrays.

const user = {
name: "Pooja",
skills: ["JavaScript", "React"],
address: {
city: "Pune",
country: "India"
}
};
const json = JSON.stringify(user);
const result = JSON.parse(json);
console.log(result.skills[0]);
console.log(result.address.city);

Output:

JavaScript
Pune

6. Special Values During JSON.stringify()

Some JavaScript values behave differently during serialization.

Undefined

When undefined is a property of an object, it is omitted.

const user = {
name: "Pooja",
age: undefined
};
console.log(JSON.stringify(user));

Output:

{"name":"Pooja"}

But when undefined appears inside an array, it becomes null.

console.log(JSON.stringify(["Java", undefined, "React"]));

Output:

["Java",null,"React"]

Functions

Functions are omitted from objects.

const user = {
name: "Pooja",
greet: function() {
return "Hello";
}
};
console.log(JSON.stringify(user));

Output:

{"name":"Pooja"}

A function inside an array becomes null.

NaN and Infinity

NaN, Infinity, and -Infinity become null.

const values = {
score: NaN,
positive: Infinity,
negative: -Infinity
};
console.log(JSON.stringify(values));

Output:

{"score":null,"positive":null,"negative":null}

BigInt

JSON.stringify() throws a TypeError when it encounters a BigInt by default.

const value = {
id: 123n
};
JSON.stringify(value); // TypeError

Convert it explicitly if you need to serialize it:

const value = {
id: 123n.toString()
};
console.log(JSON.stringify(value));

Output:

{"id":"123"}

Circular References

JSON cannot serialize circular references.

const user = {
name: "Pooja"
};
user.self = user;
JSON.stringify(user); // TypeError

The object refers to itself, creating a circular structure.

7. Handling Invalid JSON

JSON.parse() throws a SyntaxError if the input is not valid JSON.

const data = '{"name":"Pooja",}';
try {
const result = JSON.parse(data);
console.log(result);
} catch (error) {
console.log("Invalid JSON");
}

Output:

Invalid JSON

Use try...catch when parsing JSON that may be invalid.

8. JSON in Real Applications

Consider a frontend application requesting user information from a backend.

Get Pooja Chapole’s stories in your inbox

Join Medium for free to get updates from this writer.

The backend may send:

{
"id": 101,
"name": "Pooja",
"role": "user"
}

The frontend can access the parsed data:

const response = await fetch("/api/user");
const user = await response.json();
console.log(user.name);

response.json() reads the response body and parses it into a JavaScript value. You generally don't need to call JSON.parse() again on that result.

9. Quick Revision Table

Method / Concept                    Purpose                        Example                       Output
JSON.stringify() Converts value to JSON string JSON.stringify({a:1}) '{"a":1}'
JSON.parse() Converts JSON string to JS value JSON.parse('{"a":1}'). a1
undefined in object Property omitted JSON.stringify({a:undefined}) '{}'
undefined in array Converted to null JSON.stringify([undefined]) '[null]'
Function in object Property omitted JSON.stringify({fn:()=>{}}) '{}'
NaN Converted to null JSON.stringify(NaN) 'null'
BigInt Throws by default JSON.stringify(1n) TypeError
Circular reference Cannot serialize JSON.stringify(circularObj) TypeError

10. Key Takeaways

  1. JSON is a data exchange format.
  2. JSON keys and strings must use double quotes.
  3. JSON.stringify() converts JavaScript values into JSON strings.
  4. JSON.parse() converts valid JSON strings into JavaScript values.
  5. undefined and functions are omitted from objects but become null in arrays.
  6. NaN and Infinity become null.
  7. BigInt and circular references cause serialization errors by default.
  8. Use try...catch to handle invalid JSON parsing.
  9. In frontend applications, fetch().json() commonly parses API responses for you.

--

--