32

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?

Rubén
  • 34,714
  • 9
  • 70
  • 166
joejoeson
  • 1,107
  • 1
  • 10
  • 14
  • 3
    the accepted answer's solution is/is going to be deprecated. I suggest @Ahnkiet's answer be accepted. – antony.trupe Feb 12 '13 at 15:10
  • 1
    Related: [How to import JSON data into Google Spreadsheets?](http://webapps.stackexchange.com/q/73252/22759) at WebApps – kenorb Jan 28 '15 at 11:20

4 Answers4

56

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.

antony.trupe
  • 10,640
  • 10
  • 57
  • 84
Anh-Kiet Ngo
  • 2,151
  • 1
  • 14
  • 11
  • 2
    [JSON.parse](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/parse) – Photodeus Oct 09 '12 at 09:14
18

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")
garec
  • 487
  • 5
  • 7
15

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.

joshperry
  • 41,167
  • 16
  • 88
  • 103
Tim McNamara
  • 18,019
  • 4
  • 52
  • 83
  • 4
    +1 The `jsonParse()` method [was added in August 2010](http://code.google.com/googleapps/appsscript/release_notes.html) and it should be used instead of eval() or copy/pasted scripts. – Alex Jasmin Oct 18 '10 at 22:05
  • Thanks! Its about time they added that. – joejoeson Nov 11 '10 at 22:45
  • suggest that for sake of Google's juicy-ness this comment needs to be promoted as the accepted answer. – justSteve Jun 03 '11 at 01:18
  • 10
    The other answer is the better one, today (in 2012) we use [JSON.parse](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/parse) – Photodeus Oct 09 '12 at 09:11
  • 1
    In fact, the Google documentation at https://developers.google.com/apps-script/reference/utilities/utilities now says on jsonParse() that it is deprecated: "As of November 2013, replaced by JSON.parse()" – Max Horn Jul 28 '14 at 14:16
1

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]);
Wahyu Kristianto
  • 8,719
  • 6
  • 43
  • 68