Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse JSON response in Google App Script?

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"
  }
 ]
}
like image 324
Josh Avatar asked Nov 11 '16 16:11

Josh


People also ask

How do I add a JSON file to a Google script?

Open the script project in the Apps Script editor. At the left, click Project Settings settings. Select the Show "appsscript. json" manifest file in editor checkbox.

What is JSON parsing example?

Example - Parsing JSON Use the JavaScript function JSON.parse() to convert text into a JavaScript object: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); Make sure the text is in JSON format, or else you will get a syntax error.

Can you import a JSON file into Google Sheets?

Yes, Google Sheets can import JSON files, you can do it with Google Apps Script or third-party no-code apps like Zapier.


1 Answers

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

var response = authUrlFetch.fetch(url, options);
var data = JSON.parse(response.getContentText());
Logger.log(data.request.reportType);
like image 171
Amit Agarwal Avatar answered Oct 03 '22 15:10

Amit Agarwal