1

I'm starting to learn D3 and it seems like a powerful framework. I'm trying load the following html from simple python webserver. I can see that the .json file is loaded, but I don't see that json getting printed in the console log of chrome browser.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>D3 Tests</title>
    <script type="text/javascript" src="resources/d3.v3/d3.v3.js"></script>
</head>
<body>
    <script type="text/javascript">
    var dataset;
    console.log("Before json");
    d3.json("pie-chart-data.json",function(error,data){
        if (error) return console.warn(error);
        dataset = data;
        console.log("Dataset is: "+dataset);
    });
    console.log("After json");
   </script>
</body>
</html>

And here's the JSON file- pie-chart-data.json

[
{key: "One",y: 5},
{key: "Two",y: 2},
{key: "Three",y: 9},
{key: "Four",y: 7},
{key: "Five",y: 4},
{key: "Six",y: 3},
{key: "Seven",y: .5}
]

I see the "Before json" and "After json" getting printed to the console, but don't see the "Dataset is" log. Where am I going wrong - please help.

Thanks, K.

2
  • Can you access the json file using the browser? Commented May 6, 2013 at 2:53
  • 1
    Yes, I can access it using the browser. Commented May 6, 2013 at 3:53

1 Answer 1

4

The json file is malformed, it must have double quotes around the keys, and the numbers must begin with a digit or with a minus sign (not with a point).

[
    {"key": "One",   "y": 5},
    {"key": "Two",   "y": 2},
    {"key": "Three", "y": 9},
    {"key": "Four",  "y": 7},
    {"key": "Five",  "y": 4},
    {"key": "Six",   "y": 3},
    {"key": "Seven", "y": 0.5}
]

The complete json specification and syntax can be found in json.org.

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

Comments

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.