43

I exptect that mandrill_events only contains one object. How do I access its event-property?

var req = { mandrill_events: '[{"event":"inbound","ts":1426249238}]' }
5
  • 1
    That's a string, not an object. You'd need to parse the string into an object first with JSON.parse(str). Commented Mar 13, 2015 at 12:44
  • stackoverflow.com/questions/4935632/parse-json-in-javascript Commented Mar 13, 2015 at 12:45
  • JSON.parse(req[mandrill_events])[0].event will return "inbound". Commented Mar 13, 2015 at 12:45
  • 1
    @Mr.Polywhirl or not .. JSON.parse(req.mandrill_events)[0]["event"] would Commented Mar 13, 2015 at 12:47
  • @Hacketo: Yeah I forgot to remove the brackets... Commented Mar 13, 2015 at 12:48

7 Answers 7

45
var req = { mandrill_events: '[{"event":"inbound","ts":1426249238}]' }

console.log(Object.keys(req)[0]);

Make any Object array (req), then simply do Object.keys(req)[0] to pick the first key in the Object array.

Sign up to request clarification or add additional context in comments.

4 Comments

While answers are always welcome, including a brief explanation of how the code solves the problem is more helpful to others. It also reduces the likelihood the post will end up in the "Very-Low-Quality" queue (as this one did).
Your answer helped me out, but one thing that should be noted is that Object.keys(req)[0] will return you the id of the object in that array, so to get the actual value you need: mandrill_events[Object.keys(req)[0]].event
and how do I get the value using this code?
event and ts?
31

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:

var req = { mandrill_events: [{"event":"inbound","ts":1426249238}] };

and then req.mandrill_events[0], or if you're stuck with it being a string, parse the JSON the string contains:

var req = { mandrill_events: '[{"event":"inbound","ts":1426249238}]' };
var mandrill_events = JSON.parse(req.mandrill_events);
var result = mandrill_events[0];

1 Comment

Thanks for pointing out that [ is returned as content if the variable is string, not object
7

I'll explain this with a general example:

var obj = { name: "John", age: 30, city: "New York" };
var result = obj[Object.keys(obj)[0]];

The result variable will have the value "John"

Comments

6

the event property seems to be string first you have to parse it to json :

 var req = { mandrill_events: '[{"event":"inbound","ts":1426249238}]' };
 var event = JSON.parse(req.mandrill_events);
 var ts =  event[0].ts

Comments

1

'[{"event":"inbound","ts":1426249238}]' is a string, you cannot access any properties there. You will have to parse it to an object, with JSON.parse() and then handle it like a normal object

Comments

0

After you parse it with Javascript, try this:

mandrill_events[0].event

Comments

0

Assuming thant the content of mandrill_events is an object (not a string), you can also use shift() function:

var req = { mandrill_events: [{"event":"inbound","ts":1426249238}] };
var event-property = req.mandrill_events.shift().event;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.