-2

I have a Json object which i get from a API, so i can not change it and need to work with it like it is. I see it have a lot of backlashes but if i put the json string into a json validator i get the info its vallid.

if i use the json object in javascript i also can read it until the last part where i get a error, when you take my json string and you do the following steps, you will see no error message:

let ResponseJson = {"config":{"data":"market-pairs","limit":1,"symbol":"'LTC'","page":0},"usage":{"day":112,"month":262},"data":[{"name":"Litecoin","symbol":"LTC","total_rows":2221,"marketPairs":[{"id":83223,"asset_id":4,"exchange_id":113,"unique_key":"4:citex:LTC:USDT","1d":"{\"volume\":\"196939102.19\",\"volume_base\":\"1119444.67\",\"volume_change\":\"-29686113.31\",\"volume_base_change\":\"-142615.38\",\"price_change\":\"4.71070407\",\"price_quote_change\":\"4.26000000\"}","1d_volume":196939102.19,"1d_trades":null,"30d":"{\"volume\":\"10993069419.34\",\"volume_base\":\"46739211.09\",\"volume_change\":\"-111703700.37\",\"volume_base_change\":\"6038573.46\",\"price_change\":\"-173.96929593\",\"price_quote_change\":\"-174.42000000\"}","30d_volume":10993069419.34,"30d_trades":null,"exchange":"citex","from_symbol":"LTC","to_symbol":"USDT","price":178.270704,"type":"spot","last_updated":1622999998,"name":"Citex","exchange_lunar_id":"citex","pairing_url":"https://trade.citex.co.kr/trade/{{symbol1}}_{{symbol2}}","market_sort":"LTC_USDT","logo":"https://dkhpfm5hits1w.cloudfront.net/exchanges/citex.png"}]}]}
 
let size=Object.size(ResponseJson.data[0].marketPairs)-1;
console.log(size);
alert(typeof(ResponseJson.data[0].marketPairs[0]));

it will say you its a object and you can read also the size. But now if i try to read the value from marketPairs:

console.log(ResponseJson.data[0].marketPairs[i].1d_volume);

then i get the error message:

Uncaught SyntaxError: missing ) after argument list

i dont know currently how to slove this, have somebody a idea?

Object.size = function(obj) {
  var size = 0,
    key;
  for (key in obj) {
    if (obj.hasOwnProperty(key)) size++;
  }
  return size;
};

let ResponseJson = {"config":{"data":"market-pairs","limit":1,"symbol":"'LTC'","page":0},"usage":{"day":112,"month":262},"data":[{"name":"Litecoin","symbol":"LTC","total_rows":2221,"marketPairs":[{"id":83223,"asset_id":4,"exchange_id":113,"unique_key":"4:citex:LTC:USDT","1d":"{\"volume\":\"196939102.19\",\"volume_base\":\"1119444.67\",\"volume_change\":\"-29686113.31\",\"volume_base_change\":\"-142615.38\",\"price_change\":\"4.71070407\",\"price_quote_change\":\"4.26000000\"}","1d_volume":196939102.19,"1d_trades":null,"30d":"{\"volume\":\"10993069419.34\",\"volume_base\":\"46739211.09\",\"volume_change\":\"-111703700.37\",\"volume_base_change\":\"6038573.46\",\"price_change\":\"-173.96929593\",\"price_quote_change\":\"-174.42000000\"}","30d_volume":10993069419.34,"30d_trades":null,"exchange":"citex","from_symbol":"LTC","to_symbol":"USDT","price":178.270704,"type":"spot","last_updated":1622999998,"name":"Citex","exchange_lunar_id":"citex","pairing_url":"https://trade.citex.co.kr/trade/{{symbol1}}_{{symbol2}}","market_sort":"LTC_USDT","logo":"https://dkhpfm5hits1w.cloudfront.net/exchanges/citex.png"}]}]}
 
let size=Object.size(ResponseJson.data[0].marketPairs)-1;
console.log(size);
alert(typeof(ResponseJson.data[0].marketPairs[0]));
console.log(ResponseJson.data[0].marketPairs[i].1d_volume);

here is the code in a snippet, you can see the error happen there also

  • 2
    There is no `Object.size` method in the standard JavaScript library. – T.J. Crowder Jun 06 '21 at 22:24
  • Does this answer your question? [Javascript how to parse JSON array](https://stackoverflow.com/questions/9991805/javascript-how-to-parse-json-array) – Kinglish Jun 06 '21 at 22:24

1 Answers1

0

When you see an error like that after adding that particular line, then the syntax error is on that line. With that being said, you cannot have variable names in JavaScript start with a number. But, you can still make it work by accessing the object like a map rather than a member variable, by using bracket instead of dot notation syntax:

console.log(ResponseJson.data[0].marketPairs[i]["1d_volume"]);

That should fix your problem.

traktor
  • 17,588
  • 4
  • 32
  • 53
tkellehe
  • 659
  • 5
  • 11
  • 2
    @JamesButler In all fairness your custom Object.size method wasn't originally available for anyone to run it without an immediate error unrelated to the one you were getting – charlietfl Jun 06 '21 at 22:44
  • 1
    @JamesButler Another bit of advice when posting, proper writing does help the question. People tend to respond with more confidence that you are having a legitimate issue when the question is easy to read. Also, I was lucky in guessing your issue because I saw that the syntax highlighter in stack overflow looked off on that last bit. When it does not look right, then there is a good chance something is wrong. People put a lot of time into those things and they are rarely off. – tkellehe Jun 06 '21 at 22:51