{"id":801,"date":"2022-08-30T15:07:24","date_gmt":"2022-08-30T15:07:24","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/retrieving-references-data-from-json-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:07:24","modified_gmt":"2022-08-30T15:07:24","slug":"retrieving-references-data-from-json-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/retrieving-references-data-from-json-collection-of-common-programming-errors\/","title":{"rendered":"Retrieving References data from JSON-Collection of common programming errors"},"content":{"rendered":"<p><code>updates.message<\/code> is a string, not a JavaScript object. You can tell by the quotes around the whole attribute. JavaScript strings don&#8217;t have a <code>student<\/code> property, so you are getting <code>undefined<\/code>. You can parse out the JSON part from the string with regular expressions and then use <code>JSON.parse()<\/code> to get the JSON object. However, the student id is also in <code>updates.references[0].id<\/code> in your example.<\/p>\n<p>To get the student ID, do this:<\/p>\n<pre><code>\n<\/code><\/pre>\n<p><strong>edit:<\/strong> If you are really want to get the id out of the message, you need to parse it out somehow. If the message format will always be the same, you can try a regular expression or string splitting to get the part containing the id.<\/p>\n<pre><code>var id_part = json.updates.message.split(\" \")[0];\n\/\/parse out just the ID number in a group\nvar re = \/\\[\\[[^:]+:(\\d+)\\]\\]\/;\nvar matches = re.exec(id_part);\nvar id = matches[1];\n<\/code><\/pre>\n<p>To then get the corresponding data out of the <code>references<\/code> part, you need to loop through until you find one with the <code>id<\/code> from the message. This would work.<\/p>\n<pre><code>\/\/Ghetto old for loop for browser compatibility\nfor (var i = 0; i &lt; updates.references.length; i++) {\n    if (updates.references[i].id == id) {\n        \/\/We have found the reference we want.\n        \/\/Do stuff with that reference.\n        break;\n    }\n}\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:46:21. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>updates.message is a string, not a JavaScript object. You can tell by the quotes around the whole attribute. JavaScript strings don&#8217;t have a student property, so you are getting undefined. You can parse out the JSON part from the string with regular expressions and then use JSON.parse() to get the JSON object. However, the student [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-801","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/801","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=801"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/801\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}