Transforming an Excel document into JSON format: inspection of JSON code structure

Looking to transform Excel data into JSON format, but unsure if the current structure will be ideal for processing with D3.js. Planning to incorporate this JSON file within D3.js for visualization purposes.

Here's a snippet of the Excel file:

In the process of converting this dataset into a JSON file to seamlessly integrate with D3.js. Here's a glimpse at what's been achieved so far:

My query is: Is this organization method optimal for utilizing the data effectively with D3.js? Providing a sample output for reference:

Appreciate any insights and guidance!

Answer №1

This question may be subjective, but based on my experience, there is a more effective approach:

If you are using d3, chances are you are implementing something similar to this:

d3.selectAll('div')
  .data(entities)
.enter()
  .append('div')
  ...

The key consideration here is what constitutes your 'entities'. Are they all countries in the world? Or perhaps countries along with regions and the entire world? Alternatively, do your views only display countries within a selected region without including the region itself?

Unless the proposed JSON structure aligns perfectly with how entities are meant to be displayed, you will likely need to manipulate arrays extensively through concatenation and filtering to consolidate them into a single entities array for binding. While this approach might work, it could introduce unnecessary coupling between your code and data structure.

In my experience, maintaining a flat hierarchy, similar to that in an excel file, tends to be the most flexible and straightforward coding-wise. Rather than embedding regions into the hierarchy, keep them in a single, flat array like this:

{
  "immigration": [
    {
      "name": "All Countries"
      "values: [
        { "Year": ..., "value": ... },
        { "Year": ..., "value": ... },
        ...
      ]
    },
    {
      "name": "Africa"
      "values: [
        { "Year": ..., "value": ... },
        { "Year": ..., "value": ... },
        ...
      ]
    },
    {
      "name": "Eastern Africa"
      "continent": "Africa"
      "values": [
        { "Year": ..., "value": ... },
        { "Year": ..., "value": ... },
        ...
      ]
    },
    // More country entries
  ]
}

Despite being flat, this array retains a hierarchical structure via properties like region and sub-region.

To extract desired countries/regions, simple filtering can be employed instead of navigating the proposed hierarchy:

var africanEntities = data.immigration.filter(function(country) {
  return country.continent == "Africa";
}); // Includes the region "East Africa"

var justCountries = data.immigration.filter(function(country) {
  return country.continent != null && country.region != null;
});

In addition, utilizing d3's d3.nest() function can effortlessly transform flat data into a hierarchical format:

var countriesByContinent = d3.nest()
  .key(function(d) { return d.continent; })
  .map(data.immigration);

var africanEntities = countriesByContinent['Africa'];

Hopefully, this provides some clarity...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

issue with AJAX POST request not functioning correctly upon form submission

Having an issue with Ajax post while trying to submit a form event. I am working with a table that is generated by opentable.php. Here is the code for opentable.php: <?php session_start(); require("dbc.php"); $memberid = $_SESSION['memberid' ...

Combining various JSON objects by nesting them together

I'm in the process of creating an endpoint where I need to combine data from four different tables into one nested object. Each table record is retrieved in JSON format using sequelize - they include information on location, service, staff, and tenant ...

Using JSON to create a line chart with multiple lines in Apexchart

After following the Apexchart Json function, I found it to work smoothly for a single line of data in a barchart. I have experimented with creating different examples from their site, and they were straightforward to understand and all successful. Now, m ...

The (ReactJS + Redux) form fails to load when the /new segment is appended to the URL

I'm currently working on a project using React, Redux, and Rails. I've encountered an issue with loading the form. In my App.js file, I have set up a Router that defines the routes. import React, { Component } from 'react'; import { Br ...

I'm having trouble understanding why I can't access the properties of a class within a function that has been passed to an Angular

Currently, I have integrated HTML 5 geolocation into an Angular component: ... export class AngularComponent { ... constructor(private db: DatabaseService) {} // this function is linked to an HTML button logCoords(message, ...

React Material UI Table - All switches toggled simultaneously

I recently integrated a react mat ui table into my application and included switches in one of the columns. However, I am encountering an issue where all the switches toggle together instead of independently. Any suggestions on how to resolve this? < ...

Learn how to efficiently process a data queue with AngularJS using Promises

Within my AngularJS application, I have a Service and multiple controllers running simultaneously. The app receives data updates from the server in JSON format through the Angular Service. To manage the vast amount of data, I need to queue it within the se ...

Tips on eliminating overlapping strokes

I'm having trouble with drawing an array of circles that are meant to intersect a series of lines. The issue I face is that if the circles overlap, a stroke appears underneath them which I want to remove. Does anyone have any suggestions on how to el ...

Unable to for jq to identify an array located in a JSON document

Having a json file for work that requires parsing in a specific format: (^)#(^)#(^)#(^)bminter@ubuntu:~$ cat jqtest { "files":[ { "BLOCK1":{ "SUBBLOCK1":{ "akey1":"avalue1", "bkey1":"bvalue1", ...

splitting xmlhttp.responsetext using loops

Currently, I have a JavaScript function that utilizes xmlhttp.responsetext to fetch data from MySQL. This data is then used to populate form fields in a dropdown menu, which has been functioning perfectly so far. However, the issue arises when I attempt to ...

Unable to retrieve elements from the eBay website using JavaScript within a Chrome extension

I recently developed a Chrome extension that scrapes all orders from an eBay orders page. It was working flawlessly last month, but suddenly I am facing issues accessing some elements. Here is the snippet of code causing trouble: let elGridComp = document ...

Mix up table data cells using Javascript/jQuery

Can anyone provide me with some helpful tips? I'm really struggling with this. My Objective I aim to create a table with two columns ("name" and "rating"), consisting of 5 rows. 2 of these rows should have a random "rating" between 6-10 2 other ro ...

JavaScript can be utilized to alter the style of a cursor

I'm currently developing a browser game and incorporating custom cursors into it. To ensure the custom cursor is displayed throughout the entire page, I've applied it in my CSS (though setting it up for 'body' occasionally reverts back ...

Supporting multiple types for matching object structures is a feature in Jest

I'm currently working on a test using jest to verify if an object key is either a string or a number. It seems like a basic task, but surprisingly there isn't much guidance in the documentation. Test Example: test('Checking asset structure ...

Identify when two calendar dates have been modified

Creating a financial report requires the user to select two dates, search_date1 and search_date2, in order for a monthly report to be generated. Initially, I developed a daily report with only one calendar, where I successfully implemented an AJAX script ...

Swap out a component for another using a higher order component (HOC perhaps?)

I have noticed that certain libraries like "framer-motion" in react utilize this syntax, for instance to include an animated H1: <motion.h1> where the h1 tag is enhanced with animations specified in the component's props. What confuses me is ho ...

Triggering this function when clicked

I am trying to figure out a way to trigger this code onClick instead of automatically. Can anyone help me with this question? Below is the JavaScript code snippet: $("input[type=radio],button").each(function () { var outputName = $(this).attr("d ...

What is the best way to limit the range slider before it reaches its maximum point?

I am currently utilizing angularjs to stop a range slider at 75%, however, the method I am using is not very efficient and is not working as desired. Is there anyone who can provide guidance on how to achieve this? Please note: I want to display a total ...

Sencha EXT json won't load unless the model is specified correctly

I am facing an issue with my Store that is connected to a model. When I dynamically load different JSON structures into this store, sometimes the values do not get loaded properly because they differ from the model. For example: **Model:** name id descr ...

Accessing Element Content Without Identifiers in JavaScript

http://jsfiddle.net/n253R/3/ Make sure to check the link above. I'm looking to change the color of numbers based on their value. This needs to be applied to multiple numbers within different Wordpress posts. Currently, I am only able to affect one n ...