0

fairly new to Javascript, but I have been trying to figure out this parsing issue for several days now and I can't seem to put my finger on what is going on. What I would like to do is get this JSON object into an array or a list of strings into Javascript so that I can use them in my code.

I have a JSON object that I am getting using this URL:

https://min-api.cryptocompare.com/data/all/coinlist

So, I am using the following to grab that object from the URL:

$.getJSON(URL, function(json){
});

to verify that this is indeed a JSON object, I used this which returned "Function Object()"

alert(json.constructor);

So at this point, I have a JSON object inside the variable "json," I would like to start accessing some of the elements. So I try the following:

alert(JSON.stringify(json.Data));

This gives me a popup with all the text from the Data element inside this JSON object, which is great, but I am trying to access the individual items inside "Data." So I try the following which I found here: Accessing JSON elements from javascript :

alert(JSON.stringify(json.Data[1])); AND alert(JSON.stringify(json[1].Data));

Which gets me an "undefined" response. I've then tried turning the "Data" part of this Object into it's own object by using the following:

var DataObj = JSON.parse(json.Data);
console.log(DataObj["Id"]);

But that gives me a "Unexpected token o in JSON at position 1" error.

So I am not sure how to access this Data. I have literally been looking for answers for days and I hate to post what I think is a very amateur question on here but I can't seem to find the answer anywhere online.

Thank you for any help.

  • You don't need to call `JSON.parse()`, that's done automatically by `$getJSON()`. It returns the object, not JSON. – Barmar Feb 01 '18 at 01:22
  • `json.Data` is an object. There's no `[1]` property in that object. Try `json.Data[42]`. – Barmar Feb 01 '18 at 01:24
  • Yea, I figured that out, but how do I access the elements? I seem to only be able to access the overall data when I stringify, but I am trying to access all the elements under "Data." – user9296987 Feb 01 '18 at 01:24
  • 1
    `json.Data[42].CoinName` should work. – Barmar Feb 01 '18 at 01:25
  • Thank you Barmar, I can't believe it took me so long to find such a simple answer. – user9296987 Feb 01 '18 at 01:32
  • "to verify that this is indeed a JSON object, I used this which returned "Function Object()"" No, this is nonsense. JSON is just a string until you parse it, and then it becomes an object. – Brad Feb 01 '18 at 01:32

1 Answers1

0

json.Data[42].CoinName was an example from Barmar that helped me. Thank you so much