JavaScript JSON Interview Questions and Answers

Prepare for questions about JSON syntax, parsing, serialization, supported values, and common limitations.

Frequently Asked Questions

Important questions are highlighted in the sections below; select one to jump to its answer.

Theoretical JavaScript Interview Questions

JSON stands for JavaScript Object Notation. It is a text format used to store and send data. Even though it looks like a JavaScript object, it is just a string with a specific structure. It is used everywhere for sending data between a server and a browser.

json

{
  "name": "Alice",
  "age": 25,
  "isStudent": false
}

JSON.stringify() converts a JavaScript value (like an object or array) into a JSON string. This is used when you want to send data to a server or save it to localStorage.

javascript

const user = { name: "Alice", age: 25 };
const jsonString = JSON.stringify(user);
console.log(jsonString); // '{"name":"Alice","age":25}'

JSON.parse() does the opposite of JSON.stringify(). It takes a JSON string and converts it back into a JavaScript object. If the string is not valid JSON, it throws a SyntaxError.

javascript

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

JSON can only represent a limited set of data types. Things JSON does not support:

  • Functions are dropped silently
  • undefined values are dropped
  • Symbol values are dropped
  • Circular references cause an error
  • Date objects are converted to strings, not back to Date when parsed
  • Map, Set, RegExp do not serialize properly

A circular reference is when an object refers back to itself. JSON.stringify() throws a TypeError if it encounters one. You can handle it by using the replacer function in stringify, or by using a library like flatted or json-stringify-safe for cases where you actually need to serialize circular structures.

javascript

const obj = { name: "Alice" };
obj.self = obj; // circular reference

JSON.stringify(obj); // TypeError: Converting circular structure to JSON

// Simple workaround using a WeakSet to track seen objects
function safeStringify(obj) {
  const seen = new WeakSet();
  return JSON.stringify(obj, (key, value) => {
    if (typeof value === "object" && value !== null) {
      if (seen.has(value)) return "[Circular]";
      seen.add(value);
    }
    return value;
  });
}

console.log(safeStringify(obj)); // {"name":"Alice","self":"[Circular]"}

The reviver is an optional second argument to JSON.parse(). It is a function that runs for each key-value pair in the parsed result and lets you transform the value before it is returned. A common use is converting date strings back into actual Date objects.

javascript

const json = '{"name":"Alice","joinedAt":"2024-01-15T00:00:00.000Z"}';

const user = JSON.parse(json, (key, value) => {
  if (key === "joinedAt") return new Date(value);
  return value;
});

console.log(user.joinedAt instanceof Date); // true

JSON is a text data format with a restricted set of values and syntax; a JavaScript object is an in-memory value. JSON text must be parsed before object properties can be accessed.

JSON.parse converts valid JSON text into JavaScript data, while JSON.stringify serializes supported JavaScript data into JSON text.

JSON.parse throws a SyntaxError for malformed text, so catch the error at the input boundary and report or recover from invalid data safely.

Object properties with undefined values are omitted, while undefined array elements serialize as null; serializing top-level undefined returns undefined.

Parsing checks syntax, not the expected shape, types, size, or business rules. Validate the resulting data before using it.

JSON syntax permits decimal numbers, but JavaScript parses them as IEEE 754 Numbers. Integers beyond Number.MAX_SAFE_INTEGER may lose precision, so use a string representation when exact large integers matter.

The round trip drops or transforms values such as functions, undefined, symbols, and special object types, and it fails on cycles. Use a cloning strategy suited to the data supported.

Treat parsed data as untrusted and validate allowed keys and shapes before merging. Avoid unsafe recursive assignment patterns that can affect application object prototypes.

JavaScript Practical and Coding Interview Questions

Problem: Given JSON text for an array of people, parse it, trim each name, retain only name and active, then output JSON text. Handle malformed JSON by logging a clear error.

javascript

const input = '[{"name":" Ada ","active":true,"secret":"x"},{"name":" Lin ","active":false}]';

try {
  const people = JSON.parse(input);
  const normalized = people.map(person => ({
    name: person.name.trim(),
    active: Boolean(person.active)
  }));
  console.log(JSON.stringify(normalized));
} catch (error) {
  console.log('Invalid JSON input');
}