Forum Discussion

david-beltran's avatar
david-beltran
Occasional Contributor
3 years ago

Parsing JSON data in Javascript

I'm trying to sort out how to parse the JSON data from a GET request using an API. The goal is to take the individual fields and save them to Project level variables to use in Property Checkpoints. T...
  • ChrisAdams's avatar
    3 years ago

    Hi,

     

    It's a bit difficult drilling into this now as I'm UK-based.  I can try and look on Monday, but in the meantime, try building the path incrementally....

    Log.Message(aqHttpResponse);
    Log.Message(aqHttpResponse.Text);
    Log.Message(aqHttpResponse.Text.search_player_all);
    Log.Message(aqHttpResponse.Text.search_player_all.queryResults);

     I also wonder whether the response has come back as a string and firstly needs to be cast to a JSON Object.  Thinking about it, that is likely.

     

    From W3 (https://www.w3schools.com/js/js_json_parse.asp)....

    const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}');
    console.log(obj.name);

     

    You could try....

    const myObj = JSON.parse(aqHttpResponse.Text);  // Cast a string to JSON Object.
    Log.message(myObj.Text)  // If you get a response here, we're winning.
    // Then try and navgigate through your object.