Parsing JSON in Google Sheets

房东的猫 提交于 2019-12-24 05:26:04

问题


I'm working with JSON for the first time, so please excuse my lack of knowledge.

I'm trying to use a JSON file to populate data in a Google Sheet. 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?

I'm trying to pull data from here:

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("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.

What I would like to do is pull the "name" into A1 and then a few of the attributes into columns, like so:

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


回答1:


How about this workaround?

Reason of issue:

When I saw the URL of https://eddb.io/archive/v6/bodies_recently.jsonl, I noticed that the extension of the file is 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. This has already been mentioned by Dimu Designs's comment. Also I could confirm that the official document says bodies_recently.jsonl is Line-delimited JSON.

Workaround:

Unfortunately, ImportJSON cannot directly parse the values of 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. In this modification, I modified the script. Please modify as follows.

From:

The following function can be seen at the line of 130 - 135 in your script editor.

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);
}

To:

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.

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);
}

Result:

Note:

  • Although this modified script works for the values from https://eddb.io/archive/v6/bodies_recently.jsonl, I'm not sure whether this modified script works for all JSON lines values. I apologize for this.

References:

  • eddb.io/api
  • JSON Lines

If I misunderstood your question and this was not the result you want, I apologize.




回答2:


I'm not with my laptop, but I see you getting the error SyntaxError: Expected end of stream at char 2028 (line 132).

I think the data you received from the URL is to long.




回答3:


you can use =IMPORTDATA(E1) and get the whole chunk into sheets and then REGEXEXTRACT all parts you need



来源:https://stackoverflow.com/questions/56047848/parsing-json-in-google-sheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!