I exptect that mandrill_events only contains one object. How do I access its event-property
?
var req = { mandrill_events: '[{"event":"inbound","ts":1426249238}]' }
Make any Object array ( req ), then simply do Object. keys(req)[0] to pick the first key in the Object array.
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.
];var index = students. findIndex(std=> std.id === 200);
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.
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.
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.
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
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.
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.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With