Node.js - Convert an object to a string using JSON.stringify

by
Jeremy Canfield |
Updated: January 26 2025
| Node.js articles
Let's say you are using console.log.
console.log(foo);
And something like this is being returned.
[object Object]
This means that foo (in this example) is JSON or a dictionary that contains key value pairs. JSON.stringify can be used to convert the JSON or dictionary object into a string so you can see structure of the object.
console.log(JSON.stringify(foo));
Which may return something like this.
{"hello": "world"}
Or if you know the structure of the object, you can reference a particular key in the object like this.
console.log(foo.hello)
Or like this.
console.log(foo['hello'])
Let's say a POST request is being made in the foo function.
async function foo() {
try {
const response = await fetch(`https://api.example.com/api/v1/sandbox`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ greeting: "Hello World" }),
});
Here is how you should be able to parse the request in a get route.
app.get("/api/v1/sandbox", async (req, res) => {
const jsonParse = JSON.parse(JSON.stringify(req.body))
console.log(jsonParse.greeting);
}
Did you find this article helpful?
If so, consider buying me a coffee over at