DEV Community

Anden Acitelli
Anden Acitelli

Posted on • Edited on • Originally published at andenacitelli.com

Parse Google Sheets JSON Export into JavaScript JSON Object

I work for Akkio, where I'm building out a no-code predictive AI platform. If you're looking to harness the power of AI without needing a data scientist, give us a try!

The Problem

Google lets you conveniently export public sheets into a JSON object using a URL like the following:

https://docs.google.com/spreadsheets/d/{SPREADSHEET_ID}/gviz/tq?tqx=out:json&sheet={SHEET_NAME}"

However, there's a bit of extra data included in the parse that messes up the JSON parse:

  • At the beginning: /O_o/\ngoogle.visualization.Query.setResponse(
  • At the end: );

The Solution

We trim the beginning and end bits off and get a perfectly usable JSON object:


const data = await fetch(URL);
let text = await data.text();
text = text.replaceAll(
  "/*O_o*/\ngoogle.visualization.Query.setResponse(",
  ""
);
text = JSON.parse(text.substring(0, text.length - 2));
Enter fullscreen mode Exit fullscreen mode

Neon image

Serverless Postgres in 300ms (❗️)

10 free databases with autoscaling, scale-to-zero, and read replicas. Start building without infrastructure headaches. No credit card needed.

Try for Free →

Top comments (0)

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay