Graham Slick Graham Slick - 2 months ago 6
Javascript Question

JSON stored in variable has keys added

I'm issuing an AJAX call from my app to send a JSON message.
I pass this variable to the call:

json_msg = {"object":"page",
"entry":[
{"id":"317614815243036",
"time":1473615625653,
"messaging":[
{"sender":{id":"1142389195826076"},
"recipient":{"id":"317614815243036"},
"timestamp":1473615625498,
"message":{
"mid":"mid.1473615625491:99adedcab35dd94768",
"seq":4741,
"text":"Hey"
}
}]
}
]
}


But when I do a
console.log(json_msg)
, I get this:

json_msg = {"object":"page",
"entry":[
0:{"id":"317614815243036", // A ZERO ADDED HERE
"time":1473615625653,
"messaging":[
0: {"sender":{id":"1142389195826076"}, // A ZERO ADDED HERE
"recipient":{"id":"317614815243036"},
"timestamp":1473615625498,
"message":{
"mid":"mid.1473615625491:99adedcab35dd94768",
"seq":4741,
"text":"Hey"
}
}]
}
]
}


There are two "0" keys that are being added straight after assigning the variable (to test, I commented out the call and just logged the variable).

This is surely a stupid mistake but I can't find the origin

Answer

The output of console.log isn't JSON.

It shows the indexes of the array elements. This is normal.