[英]Parsing JSON in Google Sheets
I'm working with JSON for the first time, so please excuse my lack of knowledge.我是第一次使用 JSON,所以请原谅我的知识不足。
I'm trying to use a JSON file to populate data in a Google Sheet.我正在尝试使用 JSON 文件在 Google 表格中填充数据。 I just don't know the right syntax.
我只是不知道正确的语法。 How can I format a JSON function to properly access the data and stop returning an error?
如何格式化 JSON 函数以正确访问数据并停止返回错误?
I'm trying to pull data from here:我试图从这里提取数据:
https://eddb.io/archive/v6/bodies_recently.jsonl https://eddb.io/archive/v6/bodies_recently.jsonl
into a Google Sheets.进入谷歌表格。
I've got the ImportJSON script loaded and I've tested it with a really small JSON file ( http://date.jsontest.com/ ) and it works as advertised, using this function:我已经加载了 ImportJSON 脚本,并使用一个非常小的 JSON 文件( http://date.jsontest.com/ )对其进行了测试,它使用以下函数进行了宣传:
=ImportJSON("http://date.jsontest.com", "/date")
However, when I try to use the same function with the JSON from eddb.io above, I can't get it to work.但是,当我尝试对上面来自 eddb.io 的 JSON 使用相同的函数时,我无法让它工作。
What I would like to do is pull the "name" into A1 and then a few of the attributes into columns, like so:我想要做的是将“名称”拉入 A1,然后将一些属性拉入列,如下所示:
name id type_name rotational_period, etc.
Here's a link to my tests: https://docs.google.com/spreadsheets/d/1gCKpLcf-ytbPNcuQIIzxp1RMy7N5K8pD02hCLnL27qQ/edit?usp=sharing这是我的测试的链接: https : //docs.google.com/spreadsheets/d/1gCKpLcf-ytbPNcuQIIzxp1RMy7N5K8pD02hCLnL27qQ/edit?usp=sharing
How about this workaround?这个解决方法怎么样?
When I saw the URL of https://eddb.io/archive/v6/bodies_recently.jsonl
, I noticed that the extension of the file is jsonl
.当我看到
https://eddb.io/archive/v6/bodies_recently.jsonl
的 URL 时,我注意到文件的扩展名是jsonl
。 So when I checked the values retrieved from https://eddb.io/archive/v6/bodies_recently.jsonl
, it was found that the values were JSON Lines.因此,当我检查从
https://eddb.io/archive/v6/bodies_recently.jsonl
检索到的值时,发现这些值是 JSON Lines。 This has already been mentioned by Dimu Designs's comment . Dimu Designs 的评论中已经提到了这一点。 Also I could confirm that the official document says
bodies_recently.jsonl
is Line-delimited JSON.我也可以确认官方文档说
bodies_recently.jsonl
是 Line-delimited JSON。
Unfortunately, ImportJSON cannot directly parse the values of JSON Lines.不幸的是,ImportJSON 不能直接解析 JSON Lines 的值。 So it is required to modify the script as a workaround.
因此需要修改脚本作为解决方法。 In your shared Spreadsheet, the script of ImportJSON is put as the container-bound script.
在您的共享电子表格中,ImportJSON 的脚本被放置为容器绑定脚本。 In this modification, I modified the script.
在这次修改中,我修改了脚本。 Please modify as follows.
请修改如下。
The following function can be seen at the line of 130 - 135 in your script editor.可以在脚本编辑器的 130 - 135 行看到以下函数。
function ImportJSONAdvanced(url, query, options, includeFunc, transformFunc) {
var jsondata = UrlFetchApp.fetch(url);
var object = JSON.parse(jsondata.getContentText());
return parseJSONObject_(object, query, options, includeFunc, transformFunc);
}
Please replace the above function to the following script, and save the script.请将上述函数替换为以下脚本,并保存脚本。 Then, please put
=ImportJSON("https://eddb.io/archive/v6/bodies_recently.jsonl", "/id")
to a cell, again.然后,请
=ImportJSON("https://eddb.io/archive/v6/bodies_recently.jsonl", "/id")
将=ImportJSON("https://eddb.io/archive/v6/bodies_recently.jsonl", "/id")
放入单元格。
function ImportJSONAdvanced(url, query, options, includeFunc, transformFunc) {
var jsondata = UrlFetchApp.fetch(url);
var object = jsondata.getContentText().match(/{[\w\s\S].+}/g).map(function(e) {return JSON.parse(e)}); // Modified
return parseJSONObject_(object, query, options, includeFunc, transformFunc);
}
https://eddb.io/archive/v6/bodies_recently.jsonl
, I'm not sure whether this modified script works for all JSON lines values.https://eddb.io/archive/v6/bodies_recently.jsonl
的值,但我不确定此修改后的脚本是否适用于所有 JSON 行值。 I apologize for this.If I misunderstood your question and this was not the result you want, I apologize.如果我误解了您的问题并且这不是您想要的结果,我深表歉意。
I'm not with my laptop, but I see you getting the error SyntaxError: Expected end of stream at char 2028 (line 132)
.我没有带笔记本电脑,但我看到您收到错误
SyntaxError: Expected end of stream at char 2028 (line 132)
。
I think the data you received from the URL is to long.我认为您从 URL 收到的数据太长了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.