MMakela
MMakela

Reputation: 167

Parsing JSON-file with Python in Django

I have query where I fetch JSON-file and want to show the data in table in HTML. I get now error: TypeError: string indices must be integers in line: 'status': item['status'],. Is the problem with outer brackets, because they are missing in json or what?

views.py code

json_obj = urllib2.urlopen(url)
data = json.load(json_obj)
results = []
for item in data:
    results.append({
        'status': item['status'],
        'device': item['device'],
       })
return render(request, 'index/index.html', {'objects_list': results})

JSON-file:

{
  “version": “3.62”,
  "treesize": 2,
  "": [
    {
      “status”: “up”,
      "device": “someDeviceName1”,
    }
    {
      “status”: “up”,
      "device": “someDeviceName2”,
    }]
}

Upvotes: 1

Views: 767

Answers (1)

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52153

I don't know whether you accidentally copied the JSON content wrong or not but what you should do is:

>>> for item in data[""]:
...     results.append({ ...

Upvotes: 2

Related Questions