Dino Geek, try to help you

How to use JSON in Node.js?


JSON, also known as JavaScript Object Notation, is usually a built-in object in many modern web browsers and web technologies, including Node.js. It is commonly used to interact with APIs, configure frameworks, among other things.

To use JSON in Node.js, follow these steps:

1. Stringify a JavaScript Object to JSON:

First, create an object in JavaScript:

```
var person = { firstName: ‘John’, lastName: ‘Doe‘
};
```

Then, using JSON.stringify(), we can convert the JavaScript object to a JSON string:

```
var json = JSON.stringify(person);
```

The `json` string now contains a JSON representation of the `person` object. If you print it out, it will look like this:

```
‘{firstName“John”,lastName“Doe”}‘
```

1. Parsing JSON Strings back to JavaScript Objects:

If you have a JSON string, you can parse it into a JavaScript object using JSON.parse():

```
var jsonString = ‘{firstName“John”,lastName“Doe”}’;
var person = JSON.parse(jsonString);
```

Now, `person` is a JavaScript object again and its properties are accessible like before:

```
console.log(person.firstName); // Will print ‘John’.
```

1. Use JSON in an HTTP request/response scenario:

You can also use JSON when dealing with HTTP responses in Node.js. For example, when receiving data in JSON format from an HTTP request, you can use JSON.parse() function to convert it into a usable JavaScript object.

When you want to send data in JSON format in an HTTP response, you can use JSON.stringify() function to convert a JavaScript object into a JSON string and send it in the response.

Receiving JSON via HTTP request:

```
const http = require(‘http’);

const server = http.createServer((req, res) => { let body = ‘’; req.on(‘data’, chunk => { body += chunk.toString(); }); req.on(‘end’, () => { const data = JSON.parse(body); console.log(data); // … });
});
server.listen(3000);
```
Sending JSON in an HTTP response:

```
const http = require(‘http’);

const server = http.createServer((req, res) => { const jsonData = { status: ‘success’, message: ‘Data received‘ };

res.setHeader(‘Content-Type’, ‘application/json’); res.end(JSON.stringify(jsonData)); });

server.listen(3000);
```


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use