-2

hello im trying to parse an api

    </div>
    <div id=test>
    </div>
  </body>
  <script type="text/javascript" src="js/jest1.js"></script>
  <script>

  var xhr = new XMLHttpRequest();
  xhr.open("GET", "apikey", false);
  xhr.send();
  console.log(xhr.status);
  console.log(xhr.response);
  var parsed = JSON.parse(xhr);
  document.getElementById('test').innerHTML = myObject.description;
</script>

when i do this without the parsing i see all the information in the console im trying to get the information out of the console and assign it to objects. in this case it's cnn news articles. This would be the api's outputs

"status": "ok", "source": "cnn", "sortBy": "top", "articles": [{
    "author": "Clarissa Ward, Waffa Munayyer, Salma Abdelaziz,  and Fiona Sibbett, CNN",
    "title": "Gasping for life: Syria's war on children",
    "description": "Harrowing new footage of the chemical attack on the town of Khan Sheikhoun reveals the depravity of the Syrian regime, reports CNN's Clarissa Ward.",
    "url": "http://www.cnn.com/2017/05/09/middleeast/syria-chemical-attack-ward/index.html",
    "urlToImage": "http://i2.cdn.cnn.com/cnnnext/dam/assets/170509143050-syria-clarissa-ward-video-hp-tease-super-tease.jpg",
    "publishedAt": "2017-05-09T20:01:16Z"
  }]

i dont believe i ever called getjson() i never really used it and am calling the parse without declaring im getting json what im trying to do is output the description from the api and right now it's displaying at undefined in the element

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
user3642695
  • 13
  • 1
  • 6

1 Answers1

2

var parsed = JSON.parse(xhr); tries to parse the XMLHttpRequest object. That will trigger converting the object to a string and then trying to parse that string as JSON, yielding "[object Object]", which is invalid JSON as of the o.

You probably wanted:

var parsed = JSON.parse(xhr.responseText);

Then look at parsed to see where the description is in the resulting object. I can't tell you, since the JSON in the question is invalid. It might be parsed.articles[0].description, in which case:

document.getElementById('test').innerHTML = parsed.articles[0].description;

But again, without valid JSON to work from, it's hard to say. Should be trivial to figure out with the debugger built into your browser, looking at the value of parsed once it's assigned.


Side note: Synchronous ajax makes for poor UX, and is essentially never necessary.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thank you this brings to do having a undefined message at the location – user3642695 May 09 '17 at 20:41
  • @user3642695: Then either the request didn't work, or the JSON is defined that way, or you're accessing the parsed JSON incorrectly. But you definitely want to parse `xhr.responseText`, not `xhr`. – T.J. Crowder May 09 '17 at 20:47
  • i never defined json how do you define it – user3642695 May 09 '17 at 20:49
  • @user3642695: I define JSON according to the [JSON standard](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf) [pdf] (or more accessibly: http://json.org). It's not something we each get to choose for ourselves. JSON has a defined meaning. – T.J. Crowder May 09 '17 at 20:51