How to generate charts from JSON data/REST endpoints

New hope! Let's say we want to plot the height and weight (nee mass) of different characters from everyone's favourite science fiction series Star Wars (the API is available here). 

We will use the endpoint  https://swapi.co/api/people/ that returns a lot of data, including the data we want to plot. 

  • So STEP 1 is to provide this in the plugin:

You need to go to Import data from URL > Import from JSON and click on Create Parameters.

Visualizer has support for authentication for JSON imports. Clicking on HEADERS will open some new fields where you can enter the details needed for authentication for that specific JSON. In this case, make sure you click on Fetch Endpoint after the authentication fields are completed.

For accessing a password-protected JSON, the authentication token can be passed in the additional headers field as key:value, click to see image.

Below is a snippet of the JSON data that is returned from the endpoint. The data we want to plot is under the  results element. Also note that the data returned is paginated (look at the element next below count) and this is the first page of many pages of data. 

{
    "count": 87, 
    "next": "https://swapi.co/api/people/?page=2", 
    "previous": null, 
    "results": [
        {
            "name": "Luke Skywalker", 
            "height": "172", 
            "mass": "77", 
            "hair_color": "blond", 
            "skin_color": "fair", 
            "eye_color": "blue", 
            "birth_year": "19BBY", 
            "gender": "male", 
            "homeworld": "https://swapi.co/api/planets/1/", 
            "films": [
                "https://swapi.co/api/films/2/", 
                "https://swapi.co/api/films/6/", 
                "https://swapi.co/api/films/3/", 
                "https://swapi.co/api/films/1/", 
                "https://swapi.co/api/films/7/"
            ], 
            "species": [
                "https://swapi.co/api/species/1/"
            ], 
            "vehicles": [
                "https://swapi.co/api/vehicles/14/", 
                "https://swapi.co/api/vehicles/30/"
            ], 
            "starships": [
                "https://swapi.co/api/starships/12/", 
                "https://swapi.co/api/starships/22/"
            ], 
            "created": "2014-12-09T13:50:51.644000Z", 
            "edited": "2014-12-20T21:17:56.891000Z", 
            "url": "https://swapi.co/api/people/1/"
        }, 
        {
            "name": "C-3PO", 
            "height": "167", 
            "mass": "75", 
            "hair_color": "n/a", 
            "skin_color": "gold", 
            "eye_color": "yellow", 
            "birth_year": "112BBY", 
            "gender": "n/a", 
            "homeworld": "https://swapi.co/api/planets/1/", 
            "films": [
                "https://swapi.co/api/films/2/", 
                "https://swapi.co/api/films/5/", 
                "https://swapi.co/api/films/4/", 
                "https://swapi.co/api/films/6/", 
                "https://swapi.co/api/films/3/", 
                "https://swapi.co/api/films/1/"
            ], 
            "species": [
                "https://swapi.co/api/species/2/"
            ], 
            "vehicles": [], 
            "starships": [], 
            "created": "2014-12-10T15:10:51.357000Z", 
            "edited": "2014-12-20T21:17:50.309000Z", 
            "url": "https://swapi.co/api/people/2/"
        }, 
        {
            "name": "R2-D2", 
            "height": "96", 
            "mass": "32", 
            "hair_color": "n/a", 
            "skin_color": "white, blue", 
            "eye_color": "red", 
            "birth_year": "33BBY", 
            "gender": "n/a", 
            "homeworld": "https://swapi.co/api/planets/8/", 
            "films": [
                "https://swapi.co/api/films/2/", 
                "https://swapi.co/api/films/5/", 
                "https://swapi.co/api/films/4/", 
                "https://swapi.co/api/films/6/", 
                "https://swapi.co/api/films/3/", 
                "https://swapi.co/api/films/1/", 
                "https://swapi.co/api/films/7/"
            ], 
            "species": [
                "https://swapi.co/api/species/2/"
            ], 
            "vehicles": [], 
            "starships": [], 
            "created": "2014-12-10T15:11:50.376000Z", 
            "edited": "2014-12-20T21:17:50.311000Z", 
            "url": "https://swapi.co/api/people/3/"
        },
...
  • So, STEP 2, is to tell the plugin where our data is in the returned JSON:

  • Now, STEP 4 (not STEP 3, young padawan) is to tell the plugin what subset of data to use for plotting the chart and to define the data types of the columns:

In the free plugin, you can only plot the first page of the results but if you are using the PRO Addon, you can also mention where to find the other pages so that your chart data is comprehensive. After all, what's the fun of plotting heights and weights when Yoda is absent from our data set! PRO Addon user are you? Guide the plugin to the pagination element, you can! 

  • To wit, STEP 3

Psst, you can change the number 5 above by using the filter below:

	add_filter( 'visualizer_json_fetch_pages', 'visualizer_json_fetch_pages', 10, 2 );
	function visualizer_json_fetch_pages( $pages, $url ) {
		if ( 'https://swapi.co/api/people/' === $url ) {
			$pages = 10;
		}
		return $pages;
	}<br>

Once you pick the columns you want to show, clicking on Save & Show Chart will show you the chart:

For true-blue Jedis who want to tinker around with our hooks, you can refer to our document here. May the force be with you!

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us