webenginious webenginious - 3 months ago 20
Javascript Question

How to alert value inside object

I have an object from which I want to alert the value "tr_name"

My object containing this value is here:

[{"cells":[{"type":"basic.Rect","position":{"x":290,"y":450},"size":{"width":90,"height":54},"angle":0,"id":"0c53af29-a635-4e74-8759-6d31472e0ffb","embeds":"","z":1,"wi_name":"","wi_displayName":"","wi_description":"","wi_join":"<None>","wi_split":"<None>","wi_performingUser":"","wi_workitem_variables":"","wi_expected_activity_time":null,"wi_expected_user_time":null,"wi_maximum_activity_time":null,"wi_initial_delay":null,"wi_time_unit":"Seconds","wi_required_transitions_for_AND_JOIN":null,"wi_custom_page":"","attrs":{"rect":{"fill":"#0000ff","width":50,"height":30,"rx":2,"ry":2,"stroke-width":1,"stroke-dasharray":"0"},"text":{"fill":"#ffffff","text":"Activity","font-size":10,"font-family":"Arial","stroke":"#000000","stroke-width":0,"font-weight":400}}},{"type":"link","source":{"id":"0c53af29-a635-4e74-8759-6d31472e0ffb"},"target":{"x":720,"y":480},"id":"7086cf67-2254-4113-a9ca-564d4dd8c0f9","embeds":"","z":2,"smooth":false,"manhattan":false,"labels":[],"attrs":{".marker-source":{"d":"M 10 0 L 0 5 L 10 10 z","transform":"scale(1)","fill":"#000000"},".marker-target":{"d":"M 10 0 L 0 5 L 10 10 z","transform":"scale(1)","fill":"#000000"},".connection":{"stroke":"#000000","stroke-width":1,"stroke-dasharray":"0"},".attributes":{"tr_name":"AAAAAAAAAAAAAAA","tr_description":"","tr_rules":"Rule 1","tr_value":"true","tr_rule_source":"BPM"}}}]},"{}","{}","{}","{}","{}"]


So far I try my alert like this with no result:

var selectedObjectDataText = JSON.stringify(this.selection.invoke('toJSON'));
var selectedObjectDataJSON = JSON.parse(selectedObjectDataText);
alert(selectedObjectDataJSON[0].[".attributes"]["tr_name"]);


How should I take it please?

Answer
  • stringify your JSON object before alert (if you want to display an object instead of a single value)
  • your path to your data was incomplete

You can try this:

alert(JSON.stringify(selectedObjectDataJSON[0].cells[1].attrs['.attributes'].tr_name));

Live demo

https://jsfiddle.net/qyyb9xdq/

Comments