I exptect that mandrill_events only contains one object. How do I access its event-property
?
JavaScript
x
2
1
var req = { mandrill_events: '[{"event":"inbound","ts":1426249238}]' }
2
Advertisement
Answer
To answer your titular question, you use [0]
to access the first element, but as it stands mandrill_events
contains a string not an array, so mandrill_events[0]
will just get you the first character, ‘[‘.
So either correct your source to:
JavaScript
1
2
1
var req = { mandrill_events: [{"event":"inbound","ts":1426249238}] };
2
and then req.mandrill_events[0]
, or if you’re stuck with it being a string, parse the JSON the string contains:
JavaScript
1
4
1
var req = { mandrill_events: '[{"event":"inbound","ts":1426249238}]' };
2
var mandrill_events = JSON.parse(req.mandrill_events);
3
var result = mandrill_events[0];
4