I have a JSON file that looks like this
{
"values": {
"a": 1,
"b": 2,
"c": 3,
"d": 4
},
"sales": [
{ "a": 0, "b": 0, "c": 0, "d": 0, "e": "karl" },
{ "a": 0, "b": 0, "c": 0, "d": 0, "e": "karl" },
{ "a": 4, "b": 10, "c": 20, "d": 30, "e": "karl" },
{ "a": 0, "b": 0, "c": 0, "d": 0, "e": "karl" }
]
}
and I am importing that via get_context_data
import json
class MyCreateView(CreateView):
def get_context_data(self, **kwargs):
context = super(MyCreateView, self).get_context_data(**kwargs)
with open('/path/to/my/JSON/file/my_json.cfg', 'r') as f:
myfile = json.load(f)
context['my_json'] = my_data
which works, when I do print myfile["sales"][0]["a"]
I get 0
and when I put {{my_json}}
into the index.html
then I get the whole array.
So now my question is how to read the values best. Do I have to create context variables for each of the values or is it possible to read the json array in my html?
I tried {{my_json["sales"][0]["a"]}}
but didn't work