I am making a simple get request to an API endpoint.
request(URL, {headers},(err,res,body) => {
if(err){return log(err)}
// object keys are sorted but body is a string, not JSON yet.
log(body);
const obj = JSON.parse(body);
// object keys are sorted. no good.
log(obj);
})
The structure of the API is as follows.
"data": {
"965841069":"cmfvFd}leMvA_F`@wDNc@"
"380131943":"whhvF|emeMr@jAbChDTVvChCdCdAtCz@xFd@zGR"
"486015769":"i}dvFnpkeMxFiC"
"646940714":"sydvFt~jeM_HrA"
"248417189":"{}dvFxpkeMwCoN"
"659848152":"oudvFdlkeMuB{K"
"688067745":"k|ivFliieM|@t@z@Id@q@KoA]g@a@Y"
"500301841":"adfvFpwleMbTqT"
"537970914":"uqivFffjeMW@"
}
As you can see the keys ( which are dynamic, I have no way of knowing in advance what it will be) are not sorted ( in terms of their integer value) which is exactly what I want.
However when I use request to make a get request I get the following result (for example):
"data":{
"543144906":"unfvF|_meMm@fF"
"554312533":"cgfvF|jleMYB{@CYGm@VWh@MhAElA"
"576762470":"eydvFh_keMMS"
"614379898":"qzevF`ukeM}GzOiAvBi@f@"
"616186189":"{}dvFxpkeMPI"
"620745528":"}nevF~aleM~HkI`FyE"
"622882868":"mmfvFnqleMa@`MEJ"
"636661460":"_dgvFpxmeMjCGd@E~Aa@zAw@`As@^_@d@g@p@aA|@eBZiAPc@\eB?
[h@oF"
}
Here the keys are sorted which is not what the API returns. I know JSON objects do not have order but is there any way to preserve the structure/order of the original object from the API in the response object?
I have attemped at getting the response as a string and then parsing it manually but there must be a more sane solution.
Note: I dont feel this is a duplicate because in my case the keys are dynamic and I have no control over the API structure ( can't change from object to array/map)