Technical skill practice

JavaScript Object Notation: Parse JSON and Inspect the Result

JSON is text used to represent data. A JavaScript object is a live value in your program. Use JSON.parse to read supported JSON text and JSON.stringify to create it; test what the conversion preserves before treating it as a general copy operation.

The task

Convert a fictional route record from text, increase its stop count, and serialize the update. Then test a trailing comma and an undefined field. The record is ordinary data, with no dates, methods or circular references.

Runnable Node 22 example

const assert = require('node:assert/strict');

const text = '{"route":"harbor","stops":3}';
const route = JSON.parse(text);
route.stops += 1;
const encoded = JSON.stringify(route);
assert.equal(typeof text, 'string');
assert.equal(route.stops, 4);
assert.equal(encoded, '{"route":"harbor","stops":4}');
assert.throws(() => JSON.parse('{"stops":3,}'), SyntaxError);
assert.equal(JSON.stringify({note: undefined, stops: 4}), '{"stops":4}');
assert.equal(JSON.stringify([undefined]), '[null]');
console.log(encoded);

Expected output and explanation

The input string stays text; parse creates the object that the program updates. Stringify produces a new string reflecting four stops. The assertions demonstrate the distinction rather than relying on the fact that both forms can look similar in a console.

The invalid-input assertion catches the trailing comma. JSON property names and string values need double quotes; JavaScript object-literal conveniences are not necessarily valid JSON. The final checks expose a lossy case: an undefined object property is omitted, while an undefined array position becomes null.

Output from the example

{"route":"harbor","stops":4}

Pitfall to check

Do not parse untrusted text without planning for failure. A successful parse also does not prove the result has the fields or types your application requires. Validate the parsed record separately; this fixture assumes the supplied route schema.

Questions, answered

Can JSON.stringify deep-copy every JavaScript object?

No. It can lose values or fail on unsupported structures. Choose a copying approach for the actual data types rather than assuming serialization preserves them all.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.