Hướng dẫn nodejs json stringify - nodejs json stringify

Nội dung bài viết

Nội dung chính

  • Video học lập trình mỗi ngày
  • Read a JSON file using fs.readFile()
  • Read a JSON file using fs.readFileSync()
  • Reading a JSON file with require()
  • Writing to a JSON file
  • Write to a JSON file using fs.writeFile()
  • Write to a JSON file using fs.writeFileSync()

Video học lập trình mỗi ngày

Read a JSON file using fs.readFile() (JavaScript Object Notation) được sử dụng rộng rãi trong việc chia sẻ dữ liệu với nhiều ngôn ngữ khác nhau. Nhờ tính đồng nhất và đơn giản dữ liệu, do đó trong các ứng dụng Nodejs cũng được thường xuyên sử dụng. Trong bài viết này, tipjs sẽ hướng dẫn cách đọc file và ghi file JSON trong Nodejs. Cung cấp cho bạn nhiều cách nhằm có thể ở hoàn cảnh nào chúng ta cũng có thể xử lý được fileJSON. 

Read a JSON file using fs.readFileSync()databases.json sẵn có dữ liệu

[
    {
        "name": "MySQL",
        "type": "RDBMS"
    },
    {
        "name": "MongoDB",
        "type": "NoSQL"
    },
    {
        "name": "Neo4j",
        "type": "Graph DB"
    }
]

Reading a JSON file with require()

Read a JSON file using fs.readFile()

Read a JSON file using fs.readFileSync()

const fs = require('fs');

fs.readFile('./databases.json', 'utf8', (err, data) => {

    if (err) {
        console.log(`Error reading file from disk: ${err}`);
    } else {

        // parse JSON string to JSON object
        const databases = JSON.parse(data);

        // print all databases
        databases.forEach(db => {
            console.log(`${db.name}: ${db.type}`);
        });
    }

});

Reading a JSON file with require()

MySQL: RDBMS
MongoDB: NoSQL
Neo4j: Graph DB

Writing to a JSON file

Read a JSON file using fs.readFileSync()

Reading a JSON file with require()

const fs = require('fs');

try {

    const data = fs.readFileSync('./databases.json', 'utf8');

    // parse JSON string to JSON object
    const databases = JSON.parse(data);

    // print all databases
    databases.forEach(db => {
        console.log(`${db.name}: ${db.type}`);
    });

} catch (err) {
    console.log(`Error reading file from disk: ${err}`);
}

Writing to a JSON file

Reading a JSON file with require()

Writing to a JSON file

const databases = require('./databases.json');

// print all databases
databases.forEach(db => {
    console.log(`${db.name}: ${db.type}`);
});

Write to a JSON file using fs.writeFile()

Writing to a JSON file

Write to a JSON file using fs.writeFile()

Write to a JSON file using fs.writeFile()

Write to a JSON file using fs.writeFileSync()

const fs = require('fs');

let user = {
    name: 'Anonystick',
    emai: '',
    age: 37,
    gender: 'Male',
    profession: 'Software Developer'
};

// convert JSON object to a string
const data = JSON.stringify(user);

// write file to disk
fs.writeFile('./user.json', data, 'utf8', (err) => {

    if (err) {
        console.log(`Error writing file: ${err}`);
    } else {
        console.log(`File is written successfully!`);
    }

});

JSON NodeJS(JavaScript Object Notation) được sử dụng rộng rãi trong việc chia sẻ dữ liệu với nhiều ngôn ngữ khác nhau. Nhờ tính đồng nhất và đơn giản dữ liệu, do đó trong các ứng dụng Nodejs cũng được thường xuyên sử dụng. Trong bài viết này, tipjs sẽ hướng dẫn cách đọc file và ghi file JSON trong Nodejs. Cung cấp cho bạn nhiều cách nhằm có thể ở hoàn cảnh nào chúng ta cũng có thể xử lý được fileJSON. user.json và muốn đọc được thì dùng JSON.stringify() nhé các bro!

Write to a JSON file using fs.writeFileSync()

JSON NodeJS(JavaScript Object Notation) được sử dụng rộng rãi trong việc chia sẻ dữ liệu với nhiều ngôn ngữ khác nhau. Nhờ tính đồng nhất và đơn giản dữ liệu, do đó trong các ứng dụng Nodejs cũng được thường xuyên sử dụng. Trong bài viết này, tipjs sẽ hướng dẫn cách đọc file và ghi file JSON trong Nodejs. Cung cấp cho bạn nhiều cách nhằm có thể ở hoàn cảnh nào chúng ta cũng có thể xử lý được fileJSON. 

const fs = require('fs');

let user = {
    name: 'Anonystick',
    emai: '',
    age: 37,
    gender: 'Male',
    profession: 'Software Developer'
};

try {

    // convert JSON object to a string
    const data = JSON.stringify(user, null, 4);

    // write file to disk
    fs.writeFileSync('./user.json', data, 'utf8');

    console.log(`File is written successfully!`);

} catch (err) {
    console.log(`Error writing file: ${err}`);
}

Giả sử chúng ta có một file databases.json sẵn có dữ liệu

Giờ chúng ta sẽ có bao nhiêu cách để đọc file này?
Tham khảo thêm: attacomsian