JSON.stringify() <input> values as numbers?

user1382306 picture user1382306 · Aug 18, 2013 · Viewed 14.2k times · Source

I am using JSON.stringify() on html <input>s to send through a websocket like so:

JSON.stringify({
    numberValue: $('#numberValue').val()
})

but it encodes $('#numberValue').val() as a String.

How can it be encoded as a Number?

Answer

Matt Bryant picture Matt Bryant · Aug 18, 2013

Convert it to an integer first.

JSON.stringify({
    numberValue: parseInt($('#numberValue').val(), 10);
})