Exploring the World of JSON in Google Spreadsheets

I'm attempting to extract JSON data from IEXAPI into Google Sheets using a function that I discovered here:

function IMPORTJSON(url,xpath){

  try{
    var res = UrlFetchApp.fetch(url);
    var content = res.getContentText();
    var json = JSON.parse(content);

    var patharray = xpath.split("/");

    for(var i=0;i<patharray.length;i++){
      json = json[patharray[i]];
    }

    if(typeof(json) === "undefined"){
      return "Node Not Available";
    } else if(typeof(json) === "object"){
      var tempArr = [];

      for(var obj in json){
        tempArr.push([obj,json[obj]]);
      }
      return tempArr;
    } else if(typeof(json) !== "object") {
      return json;
    }
  }
  catch(err){
      return "Error getting data";  
  }

}

The JSON structure that I want to access is:

    [{
    "symbol":"AAPL",
    "sector":"electronictechnology",
    "securityType":"cs",
    "bidPrice":0,
    "bidSize":0,
    "askPrice":0,
    "askSize":0,
    "lastUpdated":1587067200000,
    "lastSalePrice":286.48,
    "lastSaleSize":100,
    "lastSaleTime":1587067199848,
    "volume":357754
}]

However, when I use the function in Google Sheets, I encounter the error Node Not Available:

=IMPORTJSON("https://cloud.iexapis.com/stable/tops?token=MY_API_KEY&symbols=aapl", "symbol")

I am confused as to why this error is occurring since symbol is a root node within the JSON. Can someone please clarify?

Answer №1

The item you are dealing with is enclosed within a bracket structure.

data = data[0][pathArray[index]];

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

JSON object for source is not being accepted by the autocomplete feature

Here is the JSP page tag for an input text element: <input name="searchTextSpan" id="searchTextSpan" type="text"/> Below is the AJAX call that loads on document.ready: AUI().use("liferay-portlet-url", function(A) { var resourceURL = Liferay ...

Having issues with PHP JSON encoding due to incorrectly formatted UTF-8 characters?

When using json_encode($data) to encode a data array, I encountered an issue with a field containing Russian characters. To determine the encoding of that field, I utilized mb_detect_encoding() which revealed it was UTF-8. Despite this, it appears that t ...

Ways to turn off hover highlighting

Is there a way to disable the highlighting effect of the <select> element in HTML? When you hover over items in the dropdown list, a blue color strip moves with your mouse. I need to find a way to get rid of this effect. Here is an example of the c ...

Leveraging Angular to incorporate HTML templates from one component into another component

I am currently working with two components. The first one is a table component, identified by the selector 'table-component', which has standard filtering capabilities. The second component is designed for displaying the table on a page. This me ...

Distinguishing between production and development environments in frontend web development using Express and HTML

I am developing a web app using Express and Node.js in the backend, with HTML files being served in the front-end without any framework. To specify the location of these front-end files in the backend, I use: app.use('/public', express.static(pat ...

The getelementbyid function is unable to locate the specified button identifier

As I dive into the world of Javascript, I wanted to create a simple input form with a corresponding response field on my website. However, I encountered an issue where using a basic submit button caused the page to refresh and erase the textfields before ...

Vue-router: I prefer to selectively choose routes for generating a dynamic loop of <router-link> components

I have successfully implemented a dynamic sidebar navigation list using router-link. <template> <div class="sidebarListItem bg-light"> <router-link v-for="route in $router.options.routes" :key="rout ...

Fetch WordPress blog entries with a specific tag in JSON form

How can I access the JSON feed of a collection of wordpress posts without utilizing the JSON Rest API? More information ...

How to arrange data in angular/typescript in either ascending or descending order based on object key

Hey there! I'm fairly new to Angular and have been working on developing a COVID-19 app using Angular. This app consists of two main components - the State component and the District component. The State component displays a table listing all states, ...

Guide for creating a CORS proxy server that can handle HTTPS requests with HTTP basic authentication

For my http requests, I've been utilizing a CORS-Proxy which works well for me. However, I recently stumbled upon an API for sending emails which requires http basic authentication for https requests. I'm uncertain of how to go about implementing ...

Calculating the Mean of Numbers in JavaScript/AngularJS

Could you help me calculate the average throughput_kbps for TCP protocol in each result array from a JSON response? I am using JavaScript/AngularJS. You can refer to this JSON for more information. Thank you in advance! ...

Accessing a precise div element in a webpage

Currently, I am utilizing Vue.js and Nuxt for my web development. One issue I am facing is related to anchors. Specifically, when I navigate to mysite.com/page1#section1, I want the page to scroll to section1. In my code, I have the following snippet: < ...

Every time I attempt to insert a background image into a div using jQuery, I am consistently faced with a 404 error message

When I hit enter in my search bar, a new div is created each time. However, I am struggling to assign a background image to the created div as I keep receiving a 404 error in the console. Below is the code snippet I'm working with: function appendToD ...

Tips for utilizing loops in Node.js to store form data as objects and arrays of objects in MongoDB

Having recently started working with MongoDB, I am incorporating Node.js, Express 4, and mongoose (mongoDB) into my project. I am facing an issue when trying to save form data to mongoDB within a loop, especially because my model contains both objects and ...

Inserting an image into a <div> means adding a reduced-size image numerous times to fill the Div

I have encountered an issue while using the code provided in the CodePen link below to paste an image into a Div. The problem is that when I paste a small image, it shows up multiple times within the Div if clicked on after pasting. My goal is to display t ...

How can you make an element be positioned in the center

Suppose I have a series of links like: <a>foobar</a><a>foobar</a><a>foobar</a><a id="center">foobar</a><a>foobar</a> If one of them has an id="center", is it possible to position it in the cente ...

Jackson returns a null value when deserializing the java.utils.logging.Level class

I have been facing challenges while deserializing a complex bean using Jackson and Lombok builder. Although I was able to resolve serialization errors related to other custom types, I am encountering issues specifically with the deserialization of a Level ...

What is the best way to incorporate multiple vertical axes (y) into a Google chart?

I created a line graph with 2 lines that refresh every 5 seconds. Now, I'm trying to add dual vAxis on the left and right side. However, I can't seem to display the vAxis title. How can I fix this? Here is the chart option that I added: This ...

Is it feasible to simultaneously load multiple directives in AngularJS?

I currently have a UI table with 5 columns, each containing different charts rendered using various directives such as custom progress bars and margin targets. The functionality is operating smoothly at the moment. Issue: When loading the page, it takes a ...

Converting SQLite to JSON in Android: A Step-by-Step Guide

Need help with converting this structure [{destLocId=10, createdUserId=b9ab2d71-9a69-4ba3-b498-d36446a154d6, createdDate=2016-6-29 14:35:00}] to a new format like this : [{"destLocId":10, "createdUserId":b9ab2d71-9a69-4ba3-b498-d36446a154d6, "cr ...