I'm trying to delete elements from _notes that have _type as 1, but i keep getting an error and I'm not sure what it means, nor do I know how to fix it.. can anyone help me?
My trimmed JSON:
{
"_notes": [
{
"_time": 10,
"_lineIndex": 2,
"_lineLayer": 0,
"_type": 0,
"_cutDirection": 7
},
{
"_time": 12,
"_lineIndex": 2,
"_lineLayer": 0,
"_type": 1,
"_cutDirection": 1
},
{
"_time": 14,
"_lineIndex": 2,
"_lineLayer": 1,
"_type": 1,
"_cutDirection": 0
}
]
}
My python script:
#!/usr/bin/python3
import json
obj = json.load(open("ExpertPlusStandardd.dat"))
for i in range(len(obj["_notes"])):
print(obj["_notes"][i]["_type"])
if obj["_notes"][i]["_type"] == 1:
obj.pop(obj["_notes"][i])
open("test.dat", "w").write(
json.dumps(obj, indent=4, separators=(',', ': '))
)
Error:
Traceback (most recent call last): File "C:\programming\python\train_one_hand\run.py", line 9, in <module> obj.pop(obj["_notes"][i]) TypeError: unhashable type: 'dict'
"ExpertPlusStandardd.dat"pop) item from object which you use infor-loop - because it moves items on list and later it tries to use index which doesn't exists any more. You should rather create new JSON and put in this JSON elements which you want to keep