javascriptjsongoogle-apps-scriptgooglebot

How to slow read output from UrlFetchApp/ waiting function till get respond from url (Google Apps Script)


var api_url = 'dns.google/resolve?name=site.com';
var response = UrlFetchApp.fetch(api_url);
var responseText = response.getContentText();
return {text : responseText};

When I have ran this, output will get this

{"Status": 0,"TC": false,"RD": true,"RA": true,"AD": false,"CD": false,"Question":[ {"name": "site.com.","type": 1}],"Answer":[ {"name": "site.com.","type": 1,"TTL": 299,"data": "204.74.99.100"}],"Comment": "Response from 204.13.250.39."}

I need this output, by parsing.

[ {"name": "site.com."
   "type": 1
   "TTL": 299
   "data": "204.74.99.100"
} ]

But in actual case its note getting output.even though running this through bash scripts will get responses,

$]curl -s https://dns.google/resolve?name=site.com

{"Status": 0,"TC": false,"RD": true,"RA": true,"AD": false,"CD": false,"Question":[ {"name": "site.com.","type": 1}],"Answer":[ {"name": "site.com.","type": 1,"TTL": 188,"data": "204.74.99.100"}]}

What can be done here, can someone suggest I have less knowledge in Javasript and Parsing data with JSON


Solution

  • function getDatta() {
      const json = '{"Status": 0,"TC": false,"RD": true,"RA": true,"AD": false,"CD": false,"Question":[ {"name": "site.com.","type": 1}],"Answer":[ {"name": "site.com.","type": 1,"TTL": 299,"data": "204.74.99.100"}],"Comment": "Response from 204.13.250.39."}';
      const obj = JSON.parse(json);
      Logger.log(JSON.stringify(obj.Answer));
    }
    
    Execution log
    5:07:41 PM  Notice  Execution started
    5:07:41 PM  Info    [{"name":"site.com.","type":1,"TTL":299,"data":"204.74.99.100"}]
    5:07:42 PM  Notice  Execution completed