JavaScript:
Working with JSON

How to:

Parsing JSON

To convert a JSON string into a JavaScript object, use JSON.parse().

const jsonString = '{"name":"John", "age":30, "city":"New York"}';
const obj = JSON.parse(jsonString);
console.log(obj.name); // Output: John

Stringifying JavaScript Objects

To convert a JavaScript object back into a JSON string, use JSON.stringify().

const user = { name: "Jane", age: 25, city: "London" };
const jsonString = JSON.stringify(user);
console.log(jsonString); // Output: {"name":"Jane","age":25,"city":"London"}

Working with Files in Node.js

To read a JSON file and convert it into an object in a Node.js environment, you can use the fs module. This example assumes you have a file named data.json.

const fs = require('fs');

fs.readFile('data.json', 'utf-8', (err, data) => {
    if (err) throw err;
    const obj = JSON.parse(data);
    console.log(obj);
});

For writing an object to a JSON file:

const fs = require('fs');
const user = { name: "Mike", age: 22, city: "Berlin" };

fs.writeFile('user.json', JSON.stringify(user, null, 2), (err) => {
    if (err) throw err;
    console.log('Data written to file');
});

Third-Party Libraries

For complex JSON operations, frameworks and libraries like lodash can simplify tasks, but for basic operations, native JavaScript functions are often sufficient. For large scale or performance-critical applications, you can consider libraries like fast-json-stringify for faster JSON stringification or json5 for parsing and stringify using a more flexible JSON format.

Parsing with json5:

const JSON5 = require('json5');

const jsonString = '{name:"John", age:30, city:"New York"}';
const obj = JSON5.parse(jsonString);
console.log(obj.name); // Output: John

These examples cover basic operations with JSON in JavaScript, perfect for beginners transitioning from other languages and looking to handle data in web applications efficiently.