Monday, 10 April 2017

JSON keys different when using console.log(object) and iterating over the object keys

I am having some trouble with a JSON object I receive from my server. Using console.log on the JSON object correctly shows the JSON object I sent from the server, but when I try to iterate over the keys it starts acting strange. See below.

> console.log(promotions);
{"promotion1":{"color":"white","backgroundColor":"red","text":"from promotion server"},"promotion2":{"color":"purple","backgroundColor":"yellow","text":"from promotion server2"},"promotion3":{"color":"green","backgroundColor":"black","text":"from promotion server3"}}

> for (p in promotions) { console.log(p); }
0
1
2
3
4
... (continues)
264
265
266
bold
strip
stripColors
trap
zalgo
zebra
rainbow
random
america
reset
dim
italic
underline
inverse
hidden
strikethrough
black
... (continues)
magentaBG
cyanBG
whiteBG

A few more details:

I am sending a request from a ExpressJS server using http.request to another ExpressJS server, which then returns the "promotion" JSON object. I have not been able to tell if it somehow gets corrupted underway (I might have set the encoding wrong?).

Testing using the same JSON object but hardcoded instead of sending it from the other server, gives the correct response.

I can give more code if needed.

Thanks!



via ReturnToZero

No comments:

Post a Comment