On JSON.stringify()
Background
When working with a Node.js service, I intended to send a request to another service with JSON:
{
"name": "Instant noddle",
"category": null
}
However, I didn't manage to send the category
field and this is what I sent instead:
{
"name": "Instant noddle"
}
It turns out..
The category
was an undefined
in my Node.js app.
According to the MDN docs, JSON.stringify()
will omit any field with undefined
value.
Lesson learned
I spent one hour on this problem. I should have read the MDN docs earlier.
JSON.stringify()
also has some others 'peculiar' behaviors (which clearly stated in the MDN docs).
Hopefully I won't fall into the same hole again.