2

I am following this d3.js example.

The data in this example is a csv file with the following structure:

name code pop
Albania ALB 3153731
United States USA 299846449
Great Britain GBR 60244834

The data is loaded this way:

.defer(d3.csv, "https://raw.githubusercontent.com/holtzy/D3-graph-gallery/master/DATA/world_population.csv", function(d) { data.set(d.code, +d.pop); })

Now I am trying to use my own modified data in JSON format.

test.json

 [{
        "name": "Albania",
        "code": "ALB",
        "pop": "0.111"
    },

    {
        "name": "United States",
        "code": "USA",
        "pop": "0.222"
    },
    {
        "name": "Great Britain",
        "code": "GBR",
        "pop": "0.333"
    }
 ]

To do this I just load my data instead of the dummy data:

var test = "test.json"

.defer(d3.json, test , function(d) { data.set(d.code, +d.pop);})

If I console.log the csv and the json, the resulting Objects look exactly the same. But with the json no map is loaded and with the csv the map is loaded.

When I console.log the csv data like this:

.defer(d3.csv,  "https://raw.githubusercontent.com/holtzy/D3-graph-gallery/master/DATA/world_population.csv", function(d) { console.log("DATA : " , d.code)
  data.set(d.code, +d.pop);})

I see the values in the code column in the console.

If I do the same with the json:

.defer(d3.json, test, function(d) { console.log("DATA : " , d.code)
    data.set(d.code, +d.pop);})

Nothing appears in the console.

So my question is, why can I access the values in the csv with d.code and d.pop, but not in the json?

8
  • Try to convert the json to csv ? Commented Jun 4, 2021 at 16:49
  • @KunalMukherjee I am planning to use an API that outputs JSON files, so this is unfortunately not an option. Commented Jun 4, 2021 at 18:25
  • Try defer(d3.json, test , function(d) { console.log('DATA: ', d); data.set(d.code, +d.pop);}) ... What do you see in the console? Commented Jun 4, 2021 at 19:03
  • @MichaelRovinsky I see three Objects in this format: 0: Object { name: "Albania", code: "ALB", pop: "0.111" } Commented Jun 5, 2021 at 12:10
  • @gython data looks valid. Can you print the data you pass to .selectAll(...).data(data_to_print).enter() Commented Jun 5, 2021 at 12:16

2 Answers 2

1

I found out what I was doing wrong.

I cant use this part:

defer(d3.json, test, function(d) { console.log("DATA : " , d.code)
    data.set(d.code, +d.pop);})

Because I cant use a row function with d3.json.

More information in this question

Sign up to request clarification or add additional context in comments.

Comments

0

It seems to be that d in defer(d3.json, test , function(d) { is an array.

Try the following:

.defer(d3.json, test , function(d) { 
  d.forEach(({code, pop}) => data.set(code, +pop));
})

8 Comments

This solution returns an object in this form: Object { } ​ "$ALB": 0.111 ​ "$GBR": 0.333 ​ "$USA": 0.222 while the òbject which is created by parsing the csv looks like this: Object { "$USA": 0.527, "$GBR": 0.46, "$ARE": 0.72 }. In the first object which was created by parsing the json the brackets are empty.
@gython I'm not sure I understand it... What does it mean 'the brackets are empty' ? What do you get if you add console.log('LEN: ', d.length, ' TYPE: ', Array.isArray(d)) ?
Sorry I wasnt very clear there. If I console.log the resulting map which is referenced by the data variable, the results for the json and csv file look different. If you look at my previous comment you see Object { } where the brackets are empty if I parse the json like in your proposed soloution. While in the second case the key-value pairs are IN the brackets.
regarding your proposed console.log I get the following result: LEN: 3 TYPE: true
I tried to add the json but it wont laod jsfiddle.net/#&togetherjs=y1w5qTmInF
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.