Unable to access a specific component of an object once it has been converted to JSON within Google Apps Script

I've been working on creating a script to extract specific information from an API response. Here's my current code:

var response = UrlFetchApp.fetch('https://[API_URL]', options);
   Logger.log(response.getContentText());
  var firstCall = response.getContentText();
  var JsonObj = JSON.parse(firstCall);
  Logger.log(firstCall['id']);
  sheet.appendRow(['successfully connected to API.']);
  sheet.appendRow([Logger.getLog()]);

Here is an example of the response I receive from the API:

[{"id":12345678901234567,"name":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d1b4bcb0b8bd91b4bcb0b8bdffb2bebc">[email protected]</a> - someText"}]

After running the script, it logs the line mentioned above and 'undefined'. My objective is to extract only the ID from the string. Any assistance would be greatly appreciated. Thank you!

Answer №1

You're so close. To make it easier, follow these steps:

function fetchURL(){

     var response = UrlFetchApp.fetch('https://jsonplaceholder.typicode.com/users');
     var jsonData = JSON.parse(response);
       Logger.log(jsonData[1].name);

}

In this scenario, jsonData acts like an array that can be accessed using jsonData[0], jsonData[1], and jsonData[2].

To retrieve properties such as id or name, simply use the dot notation like jsonData[0].id or jsonData[1].name.

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

Leverage VueJS and VueX to seamlessly integrate firebase 9 functions within my application

I am currently in the process of developing a blog application using Firebase version 9, VueJs version 3, and VueX. All functionalities such as user registration and authentication are working smoothly. However, I encountered an error when attempting to a ...

What are some strategies to prevent django form fields from being reset or cleared in the event of an error during submission?

I'm utilizing django's registration-redux for user registration, but I'm facing an issue. When I enter the same user ID, it displays an error and clears all the input fields on the form. How can I prevent this error from occurring without cl ...

Issues arise when attempting to make a SOAP request in NodeJS, as opposed to PHP where it functions seamlessly

As I work on integrating a SOAP-API to access data, I encounter an error when trying to implement it in my ExpressJS-Service using NodeJS. The API documentation provides examples in PHP, which is not my preferred language. My PHP implementation works flawl ...

Encountered a Python interpreter error when attempting to load a JSON file with the json.load() function

In the realm of Python programming, I have crafted this code to delve into a JSON file. import os import argparse import json import datetime ResultsJson = "sample.json" try: with open(ResultsJson, 'r') as j: jsonbuffer = json.loa ...

Issue with specific route causing server to throw 500 error

Recently, I have been working on a small school project that involves creating our own API and connecting it to an Angular front end. While following some tutorials, I encountered an issue where my application started throwing internal server error 500 af ...

Looking to disable the back button in the browser using next.js?

How can I prevent the browser's back button from working in next.js? // not blocked... Router.onRouteChangeStart = (url) => { return false; }; Does anyone know of a way to disable the browser's back button in next.js? ...

What is the reason behind a PHP page refresh causing a session variable to be released

In an attempt to unset a session variable after 2 minutes using unsetsession.php, I have the following code: <?php session_start(); if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 120 ...

Send an array collected from a form to the server using ReactJs

I have a task at hand where I need to work on a basic form that sends data to the server. Within this form, there is a specific field where I need to add multiple inputs and store them in an array called "users". To clarify, what I intend to do is have che ...

While making a promise, an error occurred: TypeError - Unable to access the property '0' of null

I encountered an issue when trying to assign data from a function. The error appears in the console ((in promise) TypeError: Cannot read property '0'), but the data still shows on my application. Here is the code: <template> ...

A guide to efficiently extracting the accurate ID from JSON using JSONPath syntax in Gatling

My current testing setup involves Gatling for API testing. In one of my scenarios, I need to extract an ID from a JSON response and save it in a variable. However, Gatling doesn't directly support this operation. The specific ID I am looking for corre ...

Insert the characteristics of the object into the header of the table, and populate the rows of the table

I have created an HTML table that displays specific object properties when the mouse hovers over a designated object. For example, hovering over the dog object will display the dog's name. I want to expand this functionality to also show the cat' ...

Struggling to get collapsible feature functioning on website

I'm trying to create a collapsible DIV, and I found a solution on Stack Overflow. However, I'm having trouble getting it to work on my website. I created a fiddle with the full code, and it works there. But when I implement it on my site, the con ...

The Material UI library is signaling that there is an unidentified property called `selectable` being used with the <table> tag

Whenever I try to add the selectable attribute to the Table component in Material-UI using React JS, I encounter an error. Despite checking that selectable is indeed included in TableProps, the issue persists. List of Dependencies : "material-ui": "1.0.0 ...

Promise<IDropdownOption[]> converted to <IDropdownOption[]>

I wrote a function to retrieve field values from my SPFx list: async getFieldOptions(){ const optionDrop: IDropdownOption[]= []; const variable: IEleccion = await sp.web.lists.getByTitle("Groups").fields.getByTitle("Sector").get ...

Keep rolling the dice until you hit the target number

Currently, I am in the process of learning JavaScript and one of my projects involves creating a webpage that features two dice images, an input box, and a button. The objective is for users to input a value, click the button, and then see how many rolls i ...

Combining HTML template and queryset into a JSON ajax response in Django

I need my view to return the page content and certain parameters for use in the success function of Ext.Ajax.request. views.py def importFile(request): form = ImportVectorForm() html_response = render_to_response("page_content.html", {'form& ...

Ensuring the screen reader shifts focus to the previous element

Utilizing the screen reader to redirect focus back to the previous element has proven to be a challenge for me. After clicking the Return button, it will vanish and the Submit button will take its place. If the Submit button is then clicked, it disappears ...

Obtain a Spotify Token and showcase information in next.js

This is a Simple Next.js component designed to display the currently playing song on Spotify. Context: Utilizing app Router Due to Spotify's token requirements necessitating a server-side call, the entire request is made to fetch the song from an ...

Utilizing jQuery for Populating Text Fields and Selecting Checkboxes

Is there a way to take user input and store it in a variable, then insert that information into a webpage like this one? To clarify, I'm looking for a way for an app to transfer data from a variable into an input field on a website (similar to the ex ...

Sending a JavaScript variable to PHP in order to specify the timezone

I'm working on setting the timezone for every user in the navbar.php file that's included on all pages of my website. After finding a helpful js script, I am able to echo the variable 'Europe/Brussels' to identify my timezone correctly. ...