I am writing a class to take a Chrome bookmarks file (see example below):
{
"checksum": "452bebcad611a3faffb2c009099139e5",
"roots": {
"bookmark_bar": {
"children": [ {
"date_added": "13028719861473329",
"id": "4",
"name": "first bookmark",
"type": "url",
"url": "chrome://newtab/"
}, {
"children": [ {
"children": [ {
"date_added": "13026904508000000",
"id": "7",
"name": "Getting Started",
"type": "url",
"url": "https://www.mozilla.org/en-GB/firefox/central/"
} ],
"date_added": "13028740260032410",
"date_modified": "0",
"id": "6",
"name": "Bookmarks Toolbar",
"type": "folder"
}, {
"children": [ {
"date_added": "13026904508000000",
"id": "9",
"name": "Help and Tutorials",
"type": "url",
"url": "https://www.mozilla.org/en-GB/firefox/help/"
}, {
"date_added": "13026904508000000",
"id": "10",
"name": "Customise Firefox",
"type": "url",
"url": "https://www.mozilla.org/en-GB/firefox/customize/"
}, {
"date_added": "13026904508000000",
"id": "11",
"name": "Get Involved",
"type": "url",
"url": "https://www.mozilla.org/en-GB/contribute/"
}, {
"date_added": "13026904508000000",
"id": "12",
"name": "About Us",
"type": "url",
"url": "https://www.mozilla.org/en-GB/about/"
} ],
"date_added": "13028740260032410",
"date_modified": "0",
"id": "8",
"name": "Mozilla Firefox",
"type": "folder"
}, {
"date_added": "13026904551000000",
"id": "13",
"name": "Welcome to Firefox",
"type": "url",
"url": "http://www.mozilla.org/en-US/firefox/24.0/firstrun/"
} ],
"date_added": "13028740260004410",
"date_modified": "0",
"id": "5",
"name": "Imported From Firefox",
"type": "folder"
} ],
"date_added": "13028719626916276",
"date_modified": "13028719861473329",
"id": "1",
"name": "Bookmarks bar",
"type": "folder"
},
"other": {
"children": [ ],
"date_added": "13028719626916276",
"date_modified": "0",
"id": "2",
"name": "Other bookmarks",
"type": "folder"
},
"synced": {
"children": [ ],
"date_added": "13028719626916276",
"date_modified": "0",
"id": "3",
"name": "Mobile bookmarks",
"type": "folder"
}
},
"version": 1
}
I convert from JSON to a nested dictionary then extract the bookmark urls under each relevant bookmark folder in my write_data
method.
As there can be any number of bookmark folders and/or bookmarks nested within each folder, I want to call the write_data
method within itself so that it keeps on extracting child data every time it finds a nested folder. I just can't work out how to pass the relevant child dictionaries into the same method.
I've tried building up the dictionary path with a string. I think I need to pass in a tuple or list of keys to loop through and dynamically build up the path but I can't get it working and my poor head is wrecked!
There is a similar question but the answer uses yield
which has confused me totally and was not a totally working solution anyway. Please help!
import json
import sys
import codecs
class FileExtractor(object):
def __init__(self, input_file):
self.infile = codecs.open(input_file, encoding='utf-8')
self.bookmark_data = json.load(self.infile)
def write_data(self, my_key):
for key, value in self.bookmark_data[my_key].iteritems():
if type(self.bookmark_data[my_key][key]) is dict:
print self.bookmark_data[my_key][key]['name']
for subkey, subvalue in self.bookmark_data[my_key][key].iteritems():
if subkey == "children" and len(self.bookmark_data[my_key][key][subkey]) <> 0:
print "this is a child. I can't figure out how to use write_data with this"
#self.write_data('[my_key][key][subkey]')
else:
print subkey, ": ", self.bookmark_data[my_key][key][subkey]
if(__name__=="__main__"):
stuff= FileExtractor(sys.argv[1])
stuff.write_data(('roots'))