Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access first element of JSON object array?

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

var req = { mandrill_events: '[{"event":"inbound","ts":1426249238}]' } 
like image 908
Hedge Avatar asked Mar 13 '15 12:03

Hedge


People also ask

How do I get the first element of a JSON array?

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

How do you find the first value of a JSON object?

The Object. values() will return an only array of values available in an object, now we can easily get the first value from the JSON object using array 0th index.

How do you find the index of a JSON object in an array?

];var index = students. findIndex(std=> std.id === 200);

What is toJSON () in JSON?

if you need to read or clone all of a model's data attributes, use its toJSON() method. This method returns a copy of the attributes as an object (not a JSON string despite its name). (When JSON.

What is an arrays in JSON?

Arrays as JSON Objects. Example. [ "Ford", "BMW", "Fiat" ] Arrays in JSON are almost the same as arrays in JavaScript. In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined.

How to get the first element of an array in JavaScript?

You can simply, directly access the first element of an array by index 0, for example, like so: When you try to get the first index of an empty array, you will get undefined : In ES6+, you can also use destructuring to get the first element of an array.

How to get the ID of the first item in JSON?

After you parse your JSON using parse JSON action use following expression : body ('Parse_JSON_2') [0] ['Id'] this will give you id of first item If I have answered your question, please mark your post as Solved. If you like my response, please give it a Thumbs Up. 02-14-2020 11:28 AM

How do I remove an element from an array in JSON?

The .splice () function removes an element at a specified index of the JSON array. Refer to the following code. We can use .indexOf () or includes () to check if an element is present in a simple string array. But these methods will not work for an array of objects.


2 Answers

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.

like image 126
Anuga Avatar answered Sep 23 '22 21:09

Anuga


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]; 
like image 34
stovroz Avatar answered Sep 23 '22 21:09

stovroz