开发者

Import JSON data into Google Sheets

开发者 https://www.devze.com 2022-12-27 11:22 出处:网络
I am pulling data from a web service and it开发者_开发问答 is formatted as JSON. I am writing a Google Apps Script for Google Sheets that will populate the data for me. My problem is, I can\'t seem to

I am pulling data from a web service and it开发者_开发问答 is formatted as JSON. I am writing a Google Apps Script for Google Sheets that will populate the data for me. My problem is, I can't seem to get it to parse out.

Doing:

var dataset = myJSONtext;
Browser.msgbox(dataset.item[0].key); 

errors out, saying:

item[0] is not defined.

Is there some built in way I should be doing this?


JSON.parse

For those who are seeing this in 2011+, as pointed out by Henrique Abreu at the Google support forum, Utilities.jsonParse is/will be deprecated. As you can see from the thread, there's a bug with this function that it does not work when your keys are numbers, ie "1234".

As suggested, you should be using JSON.stringify/parse.


A 2013 update -- Check out the ImportJSON library at

http://blog.fastfedora.com/projects/import-json

"ImportJSON imports data from public JSON APIs into Google Spreadsheets. It aims to operate similarly to how the native Google Spreadsheet functions ImportData and ImportXML work."

Code available here and he has submitted it to the Script Gallery: https://raw.github.com/fastfedora/google-docs/master/scripts/ImportJSON/Code.gs

Example usage: After putting the code in your Google spreadsheet's Script Editor, then paste this in cell A1 of the sheet:

=ImportJSON("http://gdata.youtube.com/feeds/api/standardfeeds/most_popular?v=2&alt=json", "/feed/entry/title,/feed/entry/content",               "noInherit,noTruncate,rawHeaders")


Apps script is (pretty much) just Javascript; plain-old JSON.parse is your best option for parsing JSON into an object representation.

You can also use JSON.stringify to serialize an object into a string representation.


Use this gist : https://gist.github.com/varun-raj/5350595a730a62ca1954

Replace

http://example.com/feeds?type=json

with your JSON url

Add your entities here

rows.push([data.id, data.name,data.email]);
0

精彩评论

暂无评论...
验证码 换一张
取 消