Transform JSON data into a Google Sheet using Google Apps Script

Having trouble inserting the JSON response into Google Sheet using Google Apps Script with the code below. Running into errors, even though I can't seem to pinpoint the issue.

Take a look at the screenshot and code snippet provided:

    function myFunction() {

  var key_67 = 'YYYYYYYYYYYYYYYYYY';
  var ss_67 = SpreadsheetApp.openById(key_67);
  var sheet_67 = ss_67.getActiveSheet();
  sheet_67.getRange('A1:AZ10000').clearContent();
 var url = 'https://creator.zoho.com/api/json/arfater/view/Leads_Report?authtoken=XXXXXXXXXXXXXXXXXXXX&scope=creatorapi&zc_ownername=ipekuet';
 var response = UrlFetchApp.fetch(url);
var json = response.getContentText();
var data = JSON.parse(json);
Logger.log(data);



  var stats=[]; //create empty array to hold data points



  //The following lines push the parsed json into empty stats array

    stats.push(data.Yearly_Sales); //temp
    stats.push(data.Email); //dewPoint
    stats.push(data.Phone); //visibility

  //append the stats array to the active sheet 
  sheet_67.appendRow(stats)

}

https://i.stack.imgur.com/2mSnE.png

Answer №1

After analyzing the JSON response from the Postman app, it appears as follows:

var zohoipekuetview65 = {"Leads":[{"Yearly_Sales":"$ 1,000.00","Email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0f7b6a7c7b4f75606760216c6062">[email protected]</a>","Phone":"123-032-03323","P‌otentially":50,"Stat‌e":"NY","ZipCode":"1‌​0036","Street":"1515 Broadway","Country":"USA","ID":"2198633000000063029","City":‌​"New York","Name":"Arfater Rahman"}]};

Upon attempting to use this response directly, an error is encountered:

function JsonResponse(){
 var json ='var zohoipekuetview65 = {"Leads":[{"Yearly_Sales":"$ 1,000.00","Email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="780c1d0b0c3802171017561b1715">[email protected]</a>","Phone":"123-032-03323","P‌​otentially":50,"Stat‌​e":"NY","ZipCode":"1‌​0036","Street":"1515 Broadway","Country":"USA","ID":"2198633000000063029","City":‌​"New York","Name":"Arfater Rahman"}]} '
 var data = JSON.parse(json);
Logger.log(data); 
}

The error message received is:

SyntaxError: Unexpected token: v

This suggests that the API response includes the term var zohoipekuetview65, which seems to be causing the issue (likely a bug).

To extract just the JSON response from the string, you can utilize the following code snippet:

function trialParse(){
var json ='var zohoipekuetview65 = {"Leads":[{"Yearly_Sales":"$ 1,000.00","Email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0470617770447e6b6c6b2a676b69">[email protected]</a>","Phone":"123-032-03323","P‌​otentially":50,"Stat‌​e":"NY","ZipCode":"1‌​0036","Street":"1515 Broadway","Country":"USA","ID":"2198633000000063029","City":‌​"New York","Name":"Arfater Rahman"}]} '
Logger.log(JsonResponse(json))
}
function JsonResponse(response){
  Logger.log(response)
  var json = response.split("=")[1]
  var data = JSON.parse(json);
  Logger.log(data);
  return data  
}

You can then simply call the function in your code using var data = JsonResponse(json).

Lastly, consider utilizing Logger.log(json) as suggested by Jordan Rhea to output the API response to your logs. You can view these logs under Views > Logs.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Out of nowhere, JavaScript/jQuery ceased to function

Everything was running smoothly on my website with jQuery Ui until I changed the color, and suddenly everything stopped working. Any ideas why this happened? I even tried writing the JavaScript within the HTML file and linking it as a separate .js file, bu ...

I am looking to sort users based on their chosen names

I have a task where I need to filter users from a list based on the name selected from another field called Select Name. The data associated with the selected name should display only the users related to that data in a field called username. Currently, wh ...

Using JQuery, identify cells located in the first column of a table excluding those in the header section

In the past, I had code that looked like this: $(elem).parents('li').find(...) I used this code when elem was an item in a list, making it easy to reference all items in the list. But now, I've made some changes and decided to use a table ...

Can you explain the distinction between the "DOMContent event" and the "load event"?

Within Chrome's Developer tool, there is a blue vertical line marked "DOMContent event fired", as well as a red line labeled "load event fired". Is it safe to assume that the "DOMContent event fired" signifies the initiation of inline JavaScript execu ...

Vue component does not display FabricJS image

Currently, I am facing an issue where I want to manipulate images on a canvas using FabricJS inside a VueJS app. In the view component, there is a prop called background which I pass in and then use fabric.Image.fromURL() to load it onto the canvas. Howeve ...

How to customize the hover background color for multicolored series in Highcharts?

Highcharts offers a background color for all columns in a series. The hover color across themes is consistently a light blue (155, 200, 255, .2). To see an example, visit: http://jsfiddle.net/38pvL4cb/ If you look at the source code, Highcharts creates a ...

Steps for Integrating Kendo Mobile's "Tap to Load More" Feature in Knockout JS

I have a series of data on one page where I'm currently retrieving data from the past two days using linq. I would like to implement a button that, when clicked, will fetch data for the next 5 days. Below is the code snippet used to retrieve data for ...

Error: You forgot to close the parenthesis after the argument list / there are duplicate items

I have already tried to review a similar question asked before by checking out this post, but unfortunately, I still couldn't find a solution for my problem. Even after attempting to add a backslash (\) before the quotation mark ("), specificall ...

Set a timer to run only during particular hours of the day, and pause until the next designated time

I need assistance with integrating a function called "tweeter" into my code so that it runs at specific times throughout the day - 09:00, 13:00, 17:00, and 21:00. Currently, the function runs continuously after the initial hour check is completed, instead ...

Tips on ensuring Angular calls you back once the view is ready

My issue arises when I update a dropdown list on one of my pages and need to trigger a refresh method on this dropdown upon updating the items. Unfortunately, I am unsure how to capture an event for this specific scenario. It seems like enlisting Angular ...

The issue where all buttons in a list are displaying the same ID

I am facing an issue with a row of buttons, all having the same class add-btn. Whenever I click on one button, I intend to log the id of its containing span. However, currently, all buttons are logging the span id of the first add-btn in the list. $(&apos ...

Trouble keeping HTML/Javascript/CSS Collapsible Menu closed after refreshing the page

My issue is that the collapsible menu I have created does not remain closed when the page is refreshed. Upon reloading the page, the collapsible menu is always fully expanded, even if it was collapsed before the refresh. This creates a problem as there is ...

Struggling with routing in Node.js while working on REST API development via HTTP

I am facing an issue while trying to complete a MEAN project. The client side is already done, but I am having trouble with the server side when attempting to make a new insertion (which is carried out using HTTP Post). Below, I will demonstrate how I hav ...

Best location to structure data within a Ruby on Rails application

I am currently developing a Rails application that will generate a set of measurements collected over several days. Each measurement is saved as an individual record with a timestamp and a value, along with additional metadata. When I request the data (wi ...

Using curl to convert data to either PHP or JSON format

Can anyone assist me? I have been attempting to execute the following command: curl -H "Authorization: Basic dXNlckBjb21wYW55LmNvbTp0ZXN0" https://security.voluum.com/login However, I am facing some challenges. Also, is it feasible to achieve this in PHP ...

Creating an element in react-draggable with two sides bound and two sides open - here's how!

At the moment, I am facing an issue where the element is restricted from moving outside of the container with the class of card-width-height. My goal is to have the right and bottom sides bounded within the container while allowing the element to move beyo ...

How can the ID of a table row be retrieved if it is selected?

In my datatable, I have a list of Cars where each row contains a Car ID. A checkbox column has been added to the first cell in the table - when checked, the row is highlighted to show the user their selection. The goal is to retrieve the IDs of all selecte ...

Updating interval time based on an external variable with jQuery

I have developed a JavaScript script where pressing a button triggers the continuous playback of an audio file. The audio, which is a 'beep' sound, serves as an alarm. The frequency at which the beep plays is determined by a setting located on a ...

What is the method for retrieving hotels from a database based on their proximity to a specific set of latitude and longitude coordinates?

I have a database table with latitude, longitude, and hotel locations. I want to create a feature that will show hotels near a specific point defined by latitude and longitude. Code Snippet function findNearbyHotels() { $this->lo ...

Does the downloading of images get affected when the CSS file has the disabled attribute?

Is it possible to delay the download of images on a website by setting the stylesheet to 'disabled'? For example: <link id="imagesCSS" rel="stylesheet" type="text/css" href="images.css" disabled> My idea is to enable the link later to tri ...