Reputation: 53
I'm trying to load a json data file in order to analyze it using the nltk framework but get an AttributeError: 'list' object has no attribute 'keys'. I have tried deleting the "json" part at the end as the documentation states that data type is autodetected by the extension of the file. Also tried deleting the database at the beginning to no avail. Any ideas where might I be stumbling?
import json
import nltk
database = nltk.data.load("data.json", "json")
Upvotes: 1
Views: 335
Reputation: 53
After hours of research, it turns out NLTK does not accept json files if the highest order is a list rather than a dict. In order to access the data, the upper most structure must be a dictionary structure with keys.
jsonfile = open('data.json')
jsonstr = jsonfile.read()
jdata = json.loads(jsonstr)[0]
This allows one to access the first element of the list which includes a dictionary inside, similar to every other element of the list. One solution is to seperate the elements of the list and load the dicts one at a time. I also suspect that while encoding the json, sort_keys = True might make the upper most structure a dictionary.
Upvotes: 1