简体   繁体   English

如何在 Google Apps 脚本中解析花括号 JSON?

[英]How to parse curly braces JSON in Google Apps Script?

I have the following output coming from an API. How can I parse through this using Google Apps Script and get that output as a Google Sheets table?我有来自 API 的以下 output。如何使用 Google Apps 脚本解析它并将 output 作为 Google 表格表格?

I tried the following, but only getting Nulls.我尝试了以下,但只得到 Nulls。

var response = UrlFetchApp.fetch(url, options);
var text = response.getContentText();

for (i in text) {
  Logger.log(text[i]);
}
{
   "status":1,
   "complete":1,
   "list":{
      "3357271691":{
         "item_id":"3357271691",
         "resolved_id":"3357271691",
         "given_url":"https:\/\/www.bloomberg.com\/news\/features\/2021-06-15\/airbnb-spends-millions-making-nightmares-at-live-anywhere-rentals-go-away",
         "given_title":"",
         "favorite":"0",
         "status":"0",
         "time_added":"1623783338",
         "time_updated":"1623783338",
         "time_read":"0",
         "time_favorited":"0",
         "sort_id":0,
         "resolved_title":"Airbnb Is Spending Millions of Dollars to Make Nightmares Go Away",
         "resolved_url":"https:\/\/www.bloomberg.com\/news\/features\/2021-06-15\/airbnb-spends-millions-making-nightmares-at-live-anywhere-rentals-go-away",
         "excerpt":"When things go horribly wrong during a stay, the company\u2019s secretive safety team jumps in to soothe guests and hosts, help families\u2014and prevent PR disasters. Megaphone.fm: How Airbnb Makes Nightmares Disappear...",
         "is_article":"1",
         "is_index":"0",
         "has_video":"0",
         "has_image":"1",
         "word_count":"155",
         "lang":"en",
         "top_image_url":"https:\/\/assets.bwbx.io\/images\/users\/iqjWHBFdfxIU\/iWjqS9z4c46I\/v0\/1200x630.jpg",
         "domain_metadata":{
            "name":"Bloomberg",
            "logo":"https:\/\/logo.clearbit.com\/bloomberg.com?size=800",
            "greyscale_logo":"https:\/\/logo.clearbit.com\/bloomberg.com?size=800&greyscale=true"
         },
         "listen_duration_estimate":60
      }
   }
}

I believe your goal is as follows.我相信你的目标如下。

  • You want to parse the retrieved values of text and put the data to Spreadsheet.您想要解析检索到的text值并将数据放入电子表格。

In this case, how about the following sample script?在这种情况下,下面的示例脚本怎么样? In this sample script, the values are parsed and create a 2-dimensional array, and the array is put to the Spreadsheet.在此示例脚本中,对值进行解析并创建一个二维数组,并将该数组放入电子表格。

Sample script:示例脚本:

function myFunction() {
  var response = UrlFetchApp.fetch(url, options);
  var text = response.getContentText();

  // I added below script.
  var header = ["item_id", "resolved_id", "given_url","given_title","favorite","status","time_added","time_updated","time_read","time_favorited","sort_id", "resolved_title","resolved_url","excerpt","is_article","is_index","has_video","has_image","word_count","lang","top_image_url", "domain_metadata.name","domain_metadata.logo","domain_metadata.greyscale_logo","listen_duration_estimate"];
  var values = [header, ...Object.values(JSON.parse(text).list).map(o => header.map(k => {
    var temp = k.split(".");
    if (temp.length == 1) {
      return o[temp[0]] || "";
    }
    return o[temp[0]] && o[temp[0]][temp[1]] ? o[temp[0]][temp[1]] : "";
  }))];
  console.log(values) // You can check the created array.
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Articles"); // Please set the sheet name.
  sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}
  • In this case, all values are put to the Spreadsheet.在这种情况下,所有值都被放入电子表格。 So, when you want to change the header, please modify it.所以,当你要更改header时,请修改它。

Note:笔记:

  • This sample script supposes that your text value is the same as the value shown in your question.此示例脚本假定您的text值与问题中显示的值相同。 When the structure of the value is different, this script might not be able to be used.当值的结构不同时,可能无法使用此脚本。 Please be careful about this.请注意这一点。

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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