Working with JSON in NodeJS

Capítulo 79

Estimated reading time: 4 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

In chapter 11 of our e-book, we are going to discuss a very important topic: Working with JSON in NodeJS. JSON, or JavaScript Object Notation, is a lightweight data exchange format that is easy for humans to read and write. It's easy for machines to parse and generate, making it a popular choice for many developers when working with APIs in NodeJS.

To start, let's see how we can create a JSON object in NodeJS. A JSON object is essentially a JavaScript object. We can create it as follows:

var object = {
    "name": "John",
    "age": 30,
    "city": "Rio de Janeiro"
};

Here, we create an object with three properties: name, age, and city. Now, if we want to convert this JavaScript object into a JSON string, we can use the JSON.stringify() function. Here's how:

var jsonString = JSON.stringify(object);

Now, jsonString is a JSON string that represents the object we created. We can verify this by doing a console.log(jsonString), which will print the JSON string to the console.

Now, let's see how we can parse a JSON string into a JavaScript object. Suppose we receive the following JSON string from an API:

Continue in our app.
  • Listen to the audio with the screen off.
  • Earn a certificate upon completion.
  • Over 5000 courses for you to explore!
Or continue reading below...
Download App

Download the app

var jsonString = '{"name": "João", "age": 30, "city": "Rio de Janeiro"}';

We can convert this JSON string into a JavaScript object using the JSON.parse() function. Here's how:

var object = JSON.parse(jsonString);

Object is now a JavaScript object that represents the JSON string that we parse. We can verify this by making a console.log(object), which will print the object to the console.

So far, we've learned how to create a JSON object, how to convert a JavaScript object to a JSON string, and how to parse a JSON string into a JavaScript object. Now, let's see how we can use these skills when working with APIs in NodeJS.

Suppose we are creating an API that allows users to obtain information about a specific user. The GET request for this API could be something like /api/users/:userId. When we receive this request, we want to return a JSON object that represents the user.

First, we need to get the userId of the request. We can do this using req.params.userId. Next, we need to fetch the user's information from our database. Once we have the user's information, we can create a JavaScript object that represents the user. Finally, we can convert this JavaScript object to a JSON string and send it back as the response to the request.

app.get('/api/users/:userId', function(req, res) {
    var userId = req.params.userId;
    // Fetch user information from database...
    var user = {
        "name": "John",
        "age": 30,
        "city": "Rio de Janeiro"
    };
    var jsonString = JSON.stringify(user);
    res.send(jsonString);
});

This is a simple example of how we can work with JSON in NodeJS when creating APIs. However, there is much more we can do with JSON in NodeJS. We can, for example, use JSON to store configurations, to save and load data from files, to send and receive data from other APIs, and much more. In the remainder of this ebook, we'll explore many of these topics in more detail.

Working with JSON is a fundamental skill for any developer working with NodeJS and APIs. We hope that this chapter has given you a good introduction to the topic and that you are eager to learn more about how you can use JSON in your own projects.

Now answer the exercise about the content:

Which of the following statements is true about working with JSON in NodeJS, according to chapter 11 of our ebook?

You are right! Congratulations, now go to the next page

You missed! Try again.

The correct statement is Option 3: We can create a JavaScript object, convert it to a JSON string, and then parse that JSON string back into a JavaScript object, as explained using JSON.stringify() and JSON.parse() functions.

Next chapter

Working with CORS in NodeJS

Arrow Right Icon
Free Ebook cover How to create APIs in NodeJS from basic to advanced
53%

How to create APIs in NodeJS from basic to advanced

5

(1)

149 pages

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.