Without changing completely the data class you want to use, this might be easisest:
def jsonSetPath(jobj, path, item):
prev = None
y = jobj
for x in path.split('/'):
prev = y
y = y[x]
prev[x] = item
A wrapper Python to descend iteratively into the object. Then you can use
jsonSetPath(data, 'foo/obj', 3)
normally. You can add this functionality to your dictionary by inheriting dict if you prefer:
class JsonDict(dict):
def __getitem__(self, path):
# We only accept strings in this dictionary
y = self
for x in path.split('/'):
y = dict.get(y, x)
return y
def __setitem__(self, path, item):
# We only accept strings in this dictionary
y = self
prev = None
for x in path.split('/'):
prev = y
y = dict.get(y, x)
prev[x] = item
note using UserDict from collections may be advised, but seems to much of a hassle without converting all the inner dictionaries to user dictionaries. Now you wrap your data (data = JsonDict(data)) and use it as you wanted. If you want to use non-strings as your keys, you need to handle that (though I am not sure that makes sense in this specific dictionary implementation).
Note only the "outer" dictionary is your custom dictionary. If the use case is more advanced you would need to convert all the inner ones as well, and then you might as well use UserDictionary.
x = s.split('/');data[x[0]][x[1]]?Foo/Bar/Baz