In my Python Django project, I am working on creating a web service and one of the tasks involves parsing a .json file.
Although the code compiles successfully, the var json_data
which is supposed to hold the data turns null when trying to access the json file.
<head>
<meta charset="UTF-8">
<title>Network Graph3</title>
<link rel="stylesheet" href="{% static 'css/style.css' %}">
<script>
// import json;
window.onload = function () {
var arr = [];
var json_data = open("{% static 'static/json/graphData.json' %}");
var obj = JSON.parse(json_data);
var i;
console.log(json_data)
if (obj == null){
return
}
for (i = 0; i < obj.documents; i++){
point = obj.documents[i];
arr[point.id].y = point.score;
}
var chart = new CanvasJS.Chart("chartContainer", {
animationEnabled: true,
theme: "light2",
title:{
text: "Dialog Sentiment Analysis"
},
axisY:{
includeZero: false
},
data: [{
type: "line",
dataPoints: arr
}]
});
chart.render();
}
</script>
</head>
The provided sample json data format:
{"documents": [{"id": "0", "score": 0.8365770578384399},
{"id": "2", "score": 0.9896875619888306},
{"id": "3", "score": 0.5},
{"id": "4", "score": 0.5},
{"id": "6", "score": 0.12722820043563843},
{"id": "7", "score": 0.16494140028953552},
{"id": "8", "score": 0.7551238536834717},
{"id": "9", "score": 0.12901419401168823},
{"id": "10", "score": 0.5},
{"id": "11", "score": 0.7559014558792114},