27

I'm using a Google AdWords/App Script and I got this response from DoubleClick Search. I'm trying to parse it to put in a Sheet/ into an array to work with and I'm not having much luck. Thank you for taking a look.

This is the original code:

var response = authUrlFetch.fetch(url, options);
 var data = JSON.stringify(response.getContentText());
 var parsedData = JSON.parse(data);

{
 "kind": "doubleclicksearch#report",
 "request": {
  "reportType": "advertiser",
  "columns": [
   {
    "columnName": "agency"
   },
   {
    "columnName": "agencyId"
   },
   {
    "columnName": "advertiser"
   },
   {
    "columnName": "advertiserId"
   }
  ],
  "includeRemovedEntities": false,
  "statisticsCurrency": "usd",
  "startRow": 0,
  "rowCount": 10000
 },
 "statisticsCurrencyCode": "USD",
 "rowCount": 2,
 "rows": [
  {
   "agency": "a",
   "agencyId": "11111111111111",
   "advertiser": "aa",
   "advertiserId": "11111111111111"
  },
  {
   "agency": "b",
   "agencyId": "222222222222222",
   "advertiser": "bb",
   "advertiserId": "22222222222"
  }
 ]
}
2
  • 3
    When I Google How to parse JSON response in Google App Script? I seem to be getting lots of results. Is none of them helpful? Commented Nov 11, 2016 at 17:01
  • 1
    I'm looking around, but I can't find how to do this with how the JSON is set up. I'm currently stuck, but still looking. Thought I'd ask. Commented Nov 11, 2016 at 17:34

2 Answers 2

68
+250

It is similar to regular JavaScript. You get the JSON response with UrlFetchApp service and then access the properties using the dot notation.

try {
    const response = UrlFetchApp.fetch(url, options);
    const data = JSON.parse(response.getContentText());
    Logger.log(data.request.reportType);
} catch (f) {
    Logger.log(f.message);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Amit, what is dot notation? I tried your code but it says TypeError: Cannot read property "reportType" from undefined - what am i doing wrong? Also had to use UrlFetchApp instead of authUrlFetch
your json retrieved does not have property type reportType. @esaruoho
3

Variation of the first answer when working with apis

var response = UrlFetchApp.fetch(externalposturl).getContentText();
var data = JSON.parse(response);
Logger.log(data.message);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.