Common JavaScript Techniques for Handling JSON: Conversion, Formatting, Replacement, and Traversal

This article provides a comprehensive guide to common JavaScript techniques for working with JSON, covering object and array structures, converting between JSON strings and objects, pretty‑printing with JSON.stringify, string replacement for log processing, and methods for iterating and recursively traversing JSON data.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
Common JavaScript Techniques for Handling JSON: Conversion, Formatting, Replacement, and Traversal

JSON (JavaScript Object Notation) is a lightweight data‑exchange format that is native to JavaScript, allowing direct manipulation without additional libraries.

In JSON there are two primary structures: objects, which are enclosed in curly braces { } and contain key/value pairs separated by commas, and arrays, which are enclosed in square brackets [ ] and hold ordered values.

Converting between a JSON string and a JavaScript object is essential for data transmission. Use JSON.parse(jsonString) to turn a string into an object, and JSON.stringify(object) to produce a JSON‑formatted string.

For pretty‑printing, JSON.stringify(value, null, 2) (where the third argument specifies the number of spaces for indentation) produces a readable, indented output.

When processing log files, you may need to replace escaped quotes. The following pattern replaces all occurrences of \" with a regular double‑quote: jsonString.replace(/\\"/g, '"') (the g flag makes the replacement global).

Iterating over JSON data is straightforward: use for (const key in obj) { … } for objects and array.forEach(item => { … }) for arrays.

Complex scenarios often require recursive traversal. The example below walks a JSON structure, removes all objects after the first one in any array, and returns the cleaned JSON:

function clean(json) {
  if (Array.isArray(json)) {
    return json.length ? [clean(json[0])] : [];
  } else if (typeof json === 'object' && json !== null) {
    const result = {};
    for (const k in json) {
      result[k] = clean(json[k]);
    }
    return result;
  }
  return json;
}

These techniques together form a practical toolkit for developers who need to read, transform, format, and analyze JSON data within JavaScript applications.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptString ManipulationData FormattingRecursive Traversal
360 Quality & Efficiency
Written by

360 Quality & Efficiency

360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.