Learn to Code via Tutorials on Repl.it

← Back to all posts
2
Save Data Using JsonStore.io
eankeen (529)

Save Data Using JsonStore.io

The jsonstore.io website has been mentioned quite a bit recently. It provides a really easy way to store and retrieve data. It's perfect if you're trying to store non-sensitive, public data.

Using jsonstore.io is super easy. First, you have to visit https://www.jsonstore.io and copy your endpoint.

If you're unfamiliar, an "endpoint" is basically some URL where you can access some service. In this case, you're accessing the jsonstore.io service.

There are two main operations that I'm going to go over

  • retrieving data
  • storing data

Retrieving Data

To retrieve data, you can simply access the endpoint URL.

As you can see, there isn't much there. (Note that I'm using a Chrome Extension called "JSON Viewer" that makes JSON data more readable)

If you want to do this programmatically, simply send a GET request to the endpoint. In Node, you can use the Axios package to easily send a GET request.

let axios = require("axios");

// Retrieves data at the URL
axios.get(url)
  .then(response => {
    let myData = response.data;
    console.log(myData);
  })
  .catch(e => console.log(e));

As you can see, we get the same output!

Storing data

If you want to store some data, you just have to send a POST request to your endpoint. For example, if you are using Axios, you can do something like this:

// Stores data at the URL
let myObject = {
  a: "alfa",
  b: "bravo",
  c: "charlie"
};

axios.post(url, myObject);

That will send the following data:

A few people in the replit community made some packages to make the process a bit easier. @TheDrone7 made a JavaScript package called async-jsonstore-io and @leon332157 made a Python module called json-store-client. If you're feeling a bit adventurous, you can use those packages!

Storing your Endpoint in an Environment Variable

In the above examples, I assigned the endpoint to a variable directly, like this:

let url = "https://www.jsonstore.io/e3b92aa0c2566780a4ff0ef6aa74f99d66ea292ac017aa98243fbbac3d5d6d62";

However, that's really bad practice since anyone that opens up our repl can find the URL, and retrieve or store data without us knowing about it. We want to hide the URL. To do this, we can utilize .env files, which will hide the information.

For Node, there is a package called dotenv. It loads up the variables in the .env file as environment variables.

All you have to do is add a require statement for it.

require("dotenv").config();

console.log(process.env.ENDPOINT);

Now, to use your variable, you can access it as a property of process.env.