DEV Community

Krzysztof Kopieczek
Krzysztof Kopieczek

Posted on

3

Simple way to transform any Google Sheet to JSON

Solution is really easy, but with one big drawback - the sheet needs to accessed publicly on the Internet. In my case this is not a permanent feature - I just have to move data from one source to another one, but I would not recommend this solution if you deal with sensitive data.

Prepare the sheet

Go to File > Publish to the web and select Entire Document > Web Page. That's all here, just close the window.
image

Next, copy the sheet id from the URL in the browser.

The saviour

gsx2json is a tool which does all the magic for us. Please go to the project website and buy a coffee to the author if it helped you. It saved me a ton of time!

Get the data

I use C# but the code is quite simple. Just create the url with the sheet id and get the JSON.

// using System.Net;
var sheetId = "1zpss4g850wpb9YltdzefCyvl2Vl4gDZ-Ip4Hk-dQjOk";
var uri = new Uri($"http://gsx2json.com/api?id={sheetId}");
string json; 

using (var wc = new WebClient())
{
    json = await wc.DownloadStringTaskAsync(uri);
}
Enter fullscreen mode Exit fullscreen mode

Here's the link to the API if you'd like to review the JSON created by gsx2json.

Transform the data

Now, when you got the data, do whatever it has to be done. Here is an example of extracting rows from the JSON response.

// using Newtonsoft.Json.Linq;
var tmp = JObject.Parse(json)["rows"];
var players = tmp.ToObject<Player[]>();
Enter fullscreen mode Exit fullscreen mode

Cleaning up

Remember to unpublish the sheet from the Internet. And that's all!

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay