I'm a beginner struggling with JSON, Dicts and Lists.
I have a JSON file, which lists songs, and I want to update each song with a generic 'list/array' item, but I get the error: AttributeError: 'dict' object has no attribute 'append'
Presumably, because it's going from a dictionary to a nested list.
I've tried a range of options, including update
, but it returns none
.
Below is a stripped-down version of the script.
How do I update 1 song? (I'll figure out the looping through the rest and dumping into the JSON file separately).
Any help appreciated.
import json
from os import path
filename = '../songbook/json/songs_v2.json'
dictObj = []
# Read JSON file
with open(filename) as fp:
dictObj = json.load(fp)
# Verify existing dict
print(dictObj)
# I can see that this is a dictionary
print(type(dictObj))
#This is what I want to add to an item
print(dictObj.append({"video": "http://www.youtube.com"}))
Here's the json.
{
"songs": [
{
"id": "1",
"song": "The Righteous and The Wicked",
"artist": "Red Hot Chili Peppers",
"key": "Gbm"
},
{
"id": "3",
"song": "Never Too Much",
"artist": "Luther Vandross",
"key": "D"
}
]
}