npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsonstat-euro

v2.1.1

Published

A library to deal with Eurostat's JSON-stat API

Downloads

236

Readme

JSON-stat for Eurostat

JSON-stat for Eurostat (EuroJSONstat, for short) is a JavaScript library to deal with Eurostat’s JSON-stat API (requests and responses). EuroJSONstat is part of the JSON-stat Toolkit. It is meant to complement, not replace, Eurostat’s own set of libraries (eurostat.js). While the latter addresses many issues, EuroJSONstat library is focused on JSON-stat-related ones.

For instance, Eurostat does not include roles in its datasets. EuroJSONstat offers a method to retrieve role-enriched datasets (fetchDataset) or allows you to add role information to an already retrieved Eurostat dataset (setRole). This is possible because Eurostat uses standardized dimension names.

JSON-stat does not provide a single way to describe status symbols. Instead, it offers the extension property to allow providers to customize their JSON-stat responses to their needs. Eurostat uses a standardized way to attach a label to each status symbol. EuroJSONstat offers a method to retrieve such information (getStatusLabel).

A design principle of the JSON-stat format is the strict separation of data and metadata in order to allow the use of exactly the same structure for full (data and metadata) responses, data-only responses and metadata-only responses. Unfortunately, Eurostat does not support metadata-only responses. EuroJSONstat offers several ways to avoid this limitation: fetchFullQuery and, since version 2.1, fetchEmptyDataset.

EuroJSONstat.fetchEmptyDataset("ei_cphi_m").then(js=>{
  if(js.class==="error"){
    console.log(`Error ${js.status} (${js.label})`);
  }else{
    console.log(js); //JSON-stat dataset with "value" as an empty array
  }
});

Finally, EuroJSONstat includes a function to convert a fully explicit query (see later) into a metadata-only dataset instance (getEmptyDataset).

Resources

Table of Contents

Queries

Queries are a special kind of object used in many EuroJSONstat functions as an argument (input query) but also as a return value (output query).

A query must contain a dataset property with a valid Eurostat dataset code:

{
  "dataset": "une_rt_a"
}

A query can optionally request information on one of the Eurostat-supported languages (default is English). A specific Eurostat API version can also be requested:

{
  "dataset": "une_rt_a",
  "lang": "fr",
  "version": "1.0"
}

Since version 2.0.0, EuroJSONstat uses Eurostat's API Statitics and the default API version is 1.0. Prior versions of EuroJSONstat used Eurostat's JSON Web Service (the default version was 2.1).

Queries can filter data in datasets. For instance, dataset une_rt_a for Austria in years 2017-2018 would require this query:

{
  "dataset": "une_rt_a",
  "filter": {
    "geo": ["AT"],
    "time": ["2017", "2018"]
  }
}

EuroJSONstat functions that return queries always include a class property with value "query" and can include label information. For instance, the previous implicit query (an input query that relies on some default parameters and values) can be tried to be translated into an explicit query (an equivalent query that includes all values requested explicitly or implicitly) using the fetchQuery function:

EuroJSONstat.fetchQuery(iq, false).then(eq=>{
  if(eq.class!=="error"){
    console.log(eq);
  }
});

fetchQuery returns a metadata-enriched query:

{
  "class": "query",
  "dataset": "une_rt_a",
  "filter": {
    "age": ["TOTAL", "Y25-74", "Y_LT25"],
    "unit": ["PC_ACT", "PC_POP", "THS_PER"],
    "sex": ["F", "M", "T"],
    "geo": ["AT"],
    "time": ["2017", "2018"]
  },
  "label": {
    "dataset": "Unemployment by sex and age - annual average",
    "dimension": {
      "age": "age",
      "unit": "unit",
      "sex": "sex",
      "geo": "geo",
      "time": "time"
    },
    "category": {
      "age": ["Total", "From 25 to 74 years", "Less than 25 years"],
      "unit": ["Percentage of active population", "Percentage of total population", "Thousand persons"],
      "sex": ["Females", "Males", "Total"],
      "geo": ["Austria"],
      "time": ["2018"]
    }
  },
  "lang": "en",
  "version": "1.0"
}

A fully explicit query is a query that exposes all the dimensions and categories of a non-filtered dataset.

EuroJSONstat.fetchFullQuery({
  "dataset": "une_rt_a",
  "lang": "fr"
}).then(eq=>{
  if(eq.class==="error"){
    console.log(`Error ${eq.status} (${eq.label})`);
  }else{
    console.log(eq);
  }
});

Filters

Another special kind of object are filters. The "filter" property of queries is a filter. Filters are made of parameters (properties of the filter object), usually dimension names, and a list of valid values (array), usually category names. For example:

{
  "geo": ["AT"],
  "time": ["2017", "2018"]
}

Some EuroJSONstat functions accept filters as arguments. For example, the addParamQuery can create a new query from a query and a filter:

EuroJSONstat.addParamQuery(
  //query
  {
    "dataset": "une_rt_a",
    "filter": {
      "geo": ["AT"],
      "time": ["2017", "2018"]
    }
  },
  //filter
  {
    "age": ["TOTAL"]
  }
);

Sample code

The following sample code tries to show the combined use of many functions in EuroJSONstat. Since version 2.1, a similar goal can be attained with the use of a single function: fetchEmptyDataset.

//A query
const query={
  dataset: "une_rt_a"
};

//Queries can be modified easily

//Add filter (Austria 2018) to previous query
const fquery=EuroJSONstat.addParamQuery(query, { geo: ["AT"], time: ["2018"] });
console.log(fquery);

//Translate query into Eurostat end-point
const url=EuroJSONstat.getURL(fquery);
console.log(url);

//Remove time and geo from previous filtered query
const uquery=EuroJSONstat.removeParamQuery(fquery, ["time", "geo"]);
console.log(uquery);
//To remove time, removeTimeQuery() is more powerful than generic removeParamQuery()

//Retrieve a Eurostat standardized JSON-stat dataset using a query
EuroJSONstat.fetchDataset(uquery).then(ds=>{
  if(ds.class==="dataset"){
    console.log(`"${query.dataset}" dataset has label "${ds.label}".`);

    const status=ds.Data(0).status;

    //Eurostat status meaning is easily retrieved
    console.log(`Status symbol of first observation is "${status}" which means "${EuroJSONstat.getStatusLabel(ds, status)}".`);

    //When standardized, Eurostat’s datasets are enriched with roles
    console.log(`Classification dimensions: ${ds.role.classification}`);    
  }
});

//Get a fully explicit query from previous query
//and convert it into a dataset instance
EuroJSONstat.fetchFullQuery(uquery).then(fq=>{
  if(fq.class==="query"){
    //Convert the fully explicit query into a dataset instance
    const ds=EuroJSONstat.getEmptyDataset(fq);
    console.log(`"${ds.extension.datasetId}" dataset has label "${ds.label}".`);
    console.log(`Classification dimensions: ${ds.role.classification}`);    
    console.log(`Available time periods: ${ds.Dimension("time", false)}`);    
    console.log(`Available geographical areas: ${ds.Dimension("geo", false)}`);    
  }
});