Skip to content

Instantly share code, notes, and snippets.

@badosa
Last active May 17, 2020 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save badosa/0e66d01e4e889c0e7eb70135ee132147 to your computer and use it in GitHub Desktop.
Save badosa/0e66d01e4e889c0e7eb70135ee132147 to your computer and use it in GitHub Desktop.
JJT ECMAScript module example

The JSON-stat Javascript Toolkit is also available as an ECMAScript module (export.mjs). This example shows how to retrieve the latest post-Brexit EU unemployment rate using the JTT module.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JJT ECMAScript module example</title>
<style>
body {
padding: 20px 40px;
font-size: 30px;
}
</style>
</head>
<body>
<main>
<p>What: <strong>Unemployment rate (%)</strong></p>
<p>Where: <strong class="geo">...</strong></p>
<p>When: <strong class="time">...</strong></p>
<p>How much: <strong class="value">...</strong></p>
</main>
<script nomodule>
//For old browsers not supporting ES modules
window.alert("Sorry, your browser does not support ECMAScript modules!");
</script>
<script type="module">
import { JSONstat } from "https://unpkg.com/jsonstat@0.13.13/export.mjs";
//or https://cdn.jsdelivr.net/npm/jsonstat@0.13.13/export.mjs
const
url="https://ec.europa.eu/eurostat/wdds/rest/data/v2.1/json/en/une_rt_a?precision=1&sex=T&lastTimePeriod=1&geo=EU27_2020&unit=PC_ACT&age=Y15-74",
main=async function(url){
const
res=await fetch(url),
json=await res.json(),
stat=JSONstat(json)
;
document.querySelector(".geo").innerText=stat.Dimension("geo").Category(0).label;
document.querySelector(".time").innerText=stat.Dimension("time").Category(0).label;
document.querySelector(".value").innerText=stat.value[0];
}
;
main(url);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment