Skip to content

Instantly share code, notes, and snippets.

@tushortz
Created September 14, 2020 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tushortz/395e265fa893266c3dbdb952fba30d17 to your computer and use it in GitHub Desktop.
Save tushortz/395e265fa893266c3dbdb952fba30d17 to your computer and use it in GitHub Desktop.
An attempt to parse json or python dict using just normal strings
# Author: Taiwo Kareem
# Attempt to parse dict by using string path
def str_data(paths, obj):
paths = paths.split(".")
for path in paths:
if path.isnumeric():
path = int(path)
obj = obj[path]
return obj
# EXAMPLE USAGE
data = {
"players": [
{ "player": "Jack Jones" },
{"player": "Mike Dean",
"hobbies": [
"swimming",
"dancing"
]
}
]
}
print(str_data("players.1.hobbies.1", data))
# result: "dancing"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment