Analysing Cloudant JSON in RunKit JavaScript Notebooks

Using the simplified cloudant-quickstart (formerly silverlining) npm module

Glynn Bird
Mar 16, 2017 · 5 min read

RunKit: bringing notebooks to JavaScript

RunKit is a notebook service that uses JavaScript as its language of choice, and it allows you to ‘require in’ any Node.js (npm) package and use that in your code. This playground environment makes it really useful to process data, document your approach, and share it with others.

Creating some data

Sign up for a Cloudant account, and in the Cloudant dashboard click Create Databases and name it cities.

Connecting to the database

Create a new RunKit notebook. In the first code block, you could write code like this:

var url = 'https://myusername:mypassword@myhost.cloudant.com/mydb';
var db = require('silverlining')(url);
var db = require('silverlining')(process.env.URL + '/cities');

Querying data

You can query your database using the query function. In this case, you’ll get all the cities whose population is greater than 5 million:

// create Cloudant connection
var db = require('silverlining')(process.env.CLOUDANTURL + '/cities');// query cities with population greater than 5 million
var cities = await db.query({'population': { '$gt': 5000000 }});
A map, automatically rendered by RunKit.
// cities in Great Britain
var cities = await db.query({'country': 'GB'});
Querying Cloudant from RunKit to return only British cities.
// get London, knowing its id
var london = await db.get('2643743');
Getting a single document id from Cloudant.
// pull two cities at once
var cities = await db.get(['2643743','2642607']);
Just enclose your id strings in an array to get multiple JSON docs.
The Properties Viewer lets you see all the underlying JSON.

Clone the notebook

Here is my cities notebook on RunKit.

Mixing with other data

You can also bring in other data sources for your notebooks. In this notebook, you’ll fetch the current position of the International Space Station and combine it with the city data from my previous example.

// get current position of the international space station
var request = require('request');
async function currentPosition() {
  var issurl = 'http://api.open-notify.org/iss-now.json';
  var obj = JSON.parse(await require('request-promise')(issurl)).iss_position;
  obj.latitude = parseFloat(obj.latitude);
  obj.longitude = parseFloat(obj.longitude);
  return obj;
};// fetch current position
var iss = await currentPosition();
Location of the ISS when I queried it earlier. Expect different results when you run this code.
// create Cloudant connection
var db = require('silverlining')(process.env.CLOUDANTURL + '/cities');// get cities "near" the ISS (sometimes there are none — there's a lot of empty out there)
var t = 5; // five degrees of tolerance
var cities = await db.query(
  { 
    'latitude': {'$gt': iss.latitude — t, '$lt': iss.latitude + t},
    'longitude': {'$gt': iss.longitude — t, '$lt': iss.longitude + t}
  }
);
I got some islands in the South Pacific. You might get an empty array if the ISS is over open seas.

What’s your next JavaScript notebook?

As a JavaScript developer, I hope you’ll get value out of analyzing data in a notebook. I chose to require my npm module, silverlining, because it simplifies database interactions with Cloudant. But remember that in RunKit, you can require in any Node.js package you need.

IBM CODAIT

Things we made with data at IBM’s Center for Open Source Data and AI Technologies.

Glynn Bird

Written by

Developer @ IBM. https://glynnbird.com

IBM CODAIT

Things we made with data at IBM’s Center for Open Source Data and AI Technologies.