When is this JSON structure getting converted to all strings? When is this JSON structure getting converted to all strings? express express

When is this JSON structure getting converted to all strings?


Your data is converted to a string as a product of sending it from client to server. Remember, the client and server are not actually communicating in JSON, they are communicating in text or binary data. The server (Express?) implicitly interprets the data sent as a string, which is converted to JSON if you include the content-type: application/json request header. You'll need explicitly to type check and convert on the server if you want the data to persist in a specific format.

TLDR (comments); don't rely on the client to send valid data. Clean it before you save it to the database.


I ran into same problem. jQuery $.post and $.ajax converts integers into strings when stringifying JSON. You can verify it by looking at the data on the server side. Server receives something like { "age": "3" }, while you really want { "age": 3 }.

The solution that works is to avoid giving jQuery pure objects, but give it a string instead:

$.ajax({ type:'POST', url: '/api/...', data: JSON.stringify(data), contentType: 'application/json'})