简体   繁体   中英

Import JSON From File - Not URL

I've been sent a JSON file which has data from an America's Cup yacht, for one race. It has 22 'paths' if that's the right term, from the sensors onboard the yacht.

Every online recommendation leads back to the script from "ImportJSON" at GitHub. However, I have not been able to adapt the functions provided to import from a File, instead of accessing a JSON feed from a URL.

I also tried to import the JSON file as a text file, but it is too large, and threw an Exception.

A public version of my spreadsheet with the ImportJSON script is at ACWS , and a copy of the json file at boat1.json (55.8Mb) as well as a zipped file at boat1.zip (3.9Mb).

Any suggestions on how to import this JSON file would be really welcome.

To use any import function, you can't use a local file. You have to use a URL. The download links from your Google file pages work just fine, though.

Google currently limits URLFetchApp calls to 50MB, so a direct import is impossible. Luckily, your zip file is much smaller than that, and the Apps Script API happens to have an unzip function.

You can adapt this apps script to fit your needs:

function ImportMyZipFile()
{
  // Your zip file URL:
  const url = 'https://drive.google.com/u/0/uc?id=1Zz5Oc_ZbK5Ohk0YRU6LxG2GdfEskf10_&export=download';

  let blob = UrlFetchApp.fetch(url).getBlob();
  // Need this or unzip will throw an error
  blob.setContentType('application/zip');

  // Get the only file in the zip archive
  let unzipped = Utilities.unzip(blob)[0];

  // Parse file contents to JS object
  let contents = unzipped.getDataAsString();
  let parsed = JSON.parse(contents);

  // Do what you want with parsed here...

  return /* data array here */;
}

parsed now holds your JavaScript object. To output to cells, call the function in the cell, and it will output the contents of your data array. I don't know exactly what data from the json file you want, but this should be enough to get you started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM