Reputation: 2248
I have a post data in the format below
{"geoJSON":{"type":"Feature","geometry":{"type":"LineString","coordinates":[[-102.038635,36.800488],[-94.567716,36.800488],[-96.149793,43.381098],[-104.060178,43.508721]]}}}
Expecting the same format on the server but received the format below
{"geoJSON":{"type":"Feature","geometry":{"type":"LineString","coordinates":[["-102.038635","36.800488"],["-94.567716","36.800488"],["-96.149793","43.381098"],["-104.060178","43.508721"]]}}}
You can see the difference in coordinates
property. Double quotes are added to each value in server received coordinates
array. How can I get the coordinates
array same as client post data?
My application configured with expressJS and using body-parser. Any additional settings required to fix the problem?
app.use(bodyParser.urlencoded({ limit: '50mb' }));
app.use(bodyParser.json({ limit: '50mb' }));
Thanks in advance.
Upvotes: 1
Views: 81
Reputation: 3487
First of all, from @bumblebeen, HTTP understands everything as a string and as such converts everything to a string. It's up to you to parse it using Number.parseInt
Also, it is irrelevant but express now ships with it's own body parser which you can use. So, rather than calling bodyParser.json
, you simply call express.json
Per the comment stated, you could have a function called parseCoordinates
function parseCoordinates(coordinates) {
return coordinates.map(inner => {
return Array.isArray(inner) ? inner.map(elem => parseFloat(elem)) : parseFloat(inner);
});
}
Upvotes: 0
Reputation: 298
Assum I have
var payload = {"geoJSON":{"type":"Feature","geometry":{"type":"LineString","coordinates":[[-102.038635,36.800488],[-94.567716,36.800488],[-96.149793,43.381098],[-104.060178,43.508721]]}}}
Using JSON.stringify(payload) to stringify this before send server.
payload = JSON.stringify(payload)
// after this code then sending payload to server
In the server using JSON.parse(payload) to use this
// server nodejs
var receivedContent = JSON.parse(payload)
// then you can using receivedContent with format you want
Upvotes: 1
Reputation: 2452
{"geoJSON":{"type":"Feature","geometry":{"type":"LineString","coordinates":[[-102.038635,36.800488],[-94.567716,36.800488],[-96.149793,43.381098],[-104.060178,43.508721]]}}}
The above is actually a JavaScript Object.
{"geoJSON":{"type":"Feature","geometry":{"type":"LineString","coordinates":[["-102.038635","36.800488"],["-94.567716","36.800488"],["-96.149793","43.381098"],["-104.060178","43.508721"]]}}}
where as the above is actually a JSON.
As one of the answer suggested, whenever you send data to server, everything in the payload will automatically be converted to strings. You should cast the values as numbers.
Also, I don't think any parse would be able to do it and you should write your own code for that. The reason I say it is because, the parser would never know if the original value was a string sent by the client or was actually a number since all the keys and values will become strings.
Upvotes: 0