title: JSON object error Uncaught TypeError: Cannot read property of undefined
body: <p>I have an issue, where for the same Object give the value, in the alert window as well as gives JS error in the browser.</p>
  
  <p>At line - "alert (obj.xtagName)" gives the error message.
  Though, I also see the correct output, in the alert dialog too.</p>
  
  <p>Quite strange for me.</p>
  
  <h2>Using</h2>
  
  <p>Browser: Chrome (v10.0.648.151)</p>
  
  <h2>Error -  Uncaught TypeError: Cannot read property 'xtagName' of undefined</h2>
  
  <p>Browser: IE (v8.0.7600.16385CO)</p>
  
  <h2>Error - 'obj.xtagName' is null or not an object</h2>
  
  <p>Object with error - obj.xtagName</p>
  
  <p>-
  I have PHP page (gXML.php) which generates JSON string. This JSON is also validated using "http://www.jsonlint.com/".
  Using AJAX call, I get this JSON and convert it into JSON Object based on responseText.</p>
  
  <h2>-Start of File: _ajax.js</h2>
  
  <h2>AJAX code call</h2>
  
  <pre><code>function gQuote_getData() {
  var request = _getHTTPObject();
  var url = "php/gXML.php";
  if (request) {
      request.onreadystatechange = function() {gQuote_parseResponse(request);};
      request.open( "GET", url, true );
      request.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 1900 00:00:00 GMT");
      request.send(null);
      return true;
  } else {
      return false;
  } }
  </code></pre>
  
  <hr>
  
  <h2>XMLRespObj</h2>
  
  <pre><code>function _getHTTPObject() {
  var xhr = false;
  if (window.XMLHttpRequest) {
      xhr = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
      try {
          xhr = new ActiveXObject("Msxml2.XMLHTTP");
      } catch(e) {
          try {
              xhr = new ActiveXObject("Microsoft.XMLHTTP");
          } catch(e) {
              xhr = false;
          }
      }
  }
  return xhr;}
  </code></pre>
  
  <hr>
  
  <h2>Receive Response</h2>
  
  <pre><code>function gQuote_parseResponse(request) {
  var resp;
  if (request.readyState == 4) {
      if (request.status == 200 || request.status == 304) {
          //Helps trim extra space at beginning and end of contruct
          resp = (request.responseText.replace(/^\s+|\s+$/g, ''));
      }
  }
  fillOutput (resp);
  //printX(gQuote_JSON);}
  </code></pre>
  
  <hr>
  
  <ul>
  <li>End of File js/_ajax.js</li>
  </ul>
  
  <p>-</p>
  
  <h2>HTML page JS script</h2>
  
  <pre><code>    function fillOutput (resp_text) {
      document.getElementById("output").value = resp_text
      printX(resp_text) }
      function printX (r_text) {
      var Xml2String = r_text;
      var myobj = (new Function("return " + Xml2String))() 
      tblvl = "xml_api_reply.finance";
      tblvl = tblvl.split (".");
      obj = myobj;
      for (var tg in tblvl )
      {
          var xtag;
          try {
              xtag = obj.xtagName;
              if (obj.xtagName == tblvl[tg])
              {
                  //alert (obj.xtagName + obj.tags.length)
                  obj = obj.tags[0]
              }
          }
          catch (e) {
              //xtag = obj.xtagName;
          }
      }
      alert (obj.xtagName)
      alert (obj.tagAttrb.data)   }
  </code></pre>
  
  <p>At line - "alert (obj.xtagName)" gives the above error message.
  Though, I also see the correct output, in the alert dialog too.</p>
  
  <p>I went through various threads in this forum, but could not locate, anything similar.
  What i have found so far is about
  - malformed JSON (w extra comma)
  - concerns about jQuery (I am not using jQuery, here)</p>
  
  <p>What is the point that I am missing here. I just a basic JS user.</p>
  
  <p>Second, both "xtagName" and "tagAttrb" are objects from the same JSON object. So, what would be causing one of them into error whereas other works fine.</p>
  
  <p>What is even more strange is I have another version of the same code, which was not well organized, there the code works fine.</p>
  
  <p>Kindly help.</p>
  
  <hr>
  
  <h2>Below is extract of JSON String</h2>
  
  <pre><code>{"xtagName":"xml_api_reply",
  "tagAttrb":{"version":"1"}
  , "tags": [
      {"xtagName":"finance",
      "tagAttrb":{"module_id":"0", "tab_id":"0", "mobile_row":"0", "mobile_zipped":"1", "row":"0", "section":"0"}
      , "tags": [
          {"xtagName":"symbol",
          "tagAttrb":{"data":".BSEREAL"}
          , "tags": []},
          {"xtagName":"pretty_symbol",
          "tagAttrb":{"data":".BSEREAL"}
          , "tags": []},
          {"xtagName":"daylight_savings",
          "tagAttrb":{"data":"true"}
          , "tags": []}]}]}
  </code></pre>