简体   繁体   中英

Is this JSON I made using jsonify with flask and python 3 formatted correctly for making a D3 graph? And if not, how should I format it?

I have a few pandas dataframes that I'm sending from flask to react in my project. This is what one of the tables looks like:

Year | Word 1 | Word 2 | Word 3
-------------------------------
1990 | 532    | 2425   | 649   
1991 | 334    | 2789   | 894
1992...etc.

Basically, I have a table that measures the uses of a few words over the years in various documents. I also have a few other tables that do other things with these words. My JSON contains an array of json objects, each one being a table that does a different thing.This is what my json is structured like:

[
  {
    "Year": {
      "0": 1990,
      "1": 1991,
      etc.
    },
    "Word 1": {
      "0": 532,
      "1": 334,
      etc.
    },
    "Word 2": {
      "0": 2425,
      "1": 2789,
      etc.
    },
    "word 3": {
      "0": 649,
      "1": 894,
      etc.
    }
  },
  {
    "Year": {
      "0": 1990,
      "1": 1991,
      etc.
    },
    "Word 1": {
      "0": 11.4,
      "1": 8.2,
      etc.
    },
    "Word 2": {
      "0": 79.3,
      "1": 84.5,
      etc.
    },
    "word 3": {
      "0": 20.3,
      "1": 24.9,
      etc.
    }
  }
]

Obviously my JSON is much bigger, has more years, and more dataframes inside of it. I have tried taking this to D3, but I'm somewhat inexperienced with D3 and the documentation has not helped to explain to me why a JSON like this can't be inputted into D3 correctly and why I can't make graphs for each of the datasets I put into this JSON. Hopefully someone who knows more about JSONs and how D3 interprets them can help me to understand why this format is incorrect or how I could go about graphing them (because to be completely honest I'm unsure of how to even do that, as documentation is confusing and often scarce). Thank you for taking your time to help me with this.

I'm interpreting here, but it looks like D3 wants a list with one object per line, like you'd get from a CSV file:

[
  {
    "Year": 1990,
    "Word 1": 531,
    "Word 2": 2425,
    "Word 3": 649
  },
  {
    "Year": 1991,
    "Word 1": 334,
    "Word 2": 2789,
    "Word 3": 894
  },
  ...
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM