Skip to content

Instantly share code, notes, and snippets.

@Tech0ne
Last active June 17, 2025 12:22
Show Gist options
  • Save Tech0ne/aee2bb7f02afb8cf59c1718ecc999e8e to your computer and use it in GitHub Desktop.
Save Tech0ne/aee2bb7f02afb8cf59c1718ecc999e8e to your computer and use it in GitHub Desktop.
Python JSON object representation
##############
#
# @
# @
# @ @ @
#
##############
def repr_json(data, is_key: bool = False, tab: int = 0) -> str:
if isinstance(data, str) and is_key:
return f"{' ' * tab}\x1b[35m\"{data}\"\x1b[0m"
elif isinstance(data, str):
return f"{' ' * tab}\x1b[32m\"{data}\"\x1b[0m"
elif isinstance(data, bool) or data is None:
return f"{' ' * tab}\x1b[36m{str(data).lower()}\x1b[0m"
elif isinstance(data, float) or isinstance(data, int):
return f"{' ' * tab}\x1b[33m{data}\x1b[0m"
elif isinstance(data, list):
output = f"{' ' * tab}[\n"
for e in data:
output += repr_json(e, tab=tab + 4)
output += ",\n"
return output + f"{' ' * tab}]"
elif isinstance(data, dict):
output = f"{' ' * tab}{{\n"
for k, v in data.items():
output += repr_json(k, is_key=True, tab=tab + 4)
output += ": "
output += repr_json(v, tab=tab + 4).lstrip()
output += ",\n"
return output + f"{' ' * tab}}}"
return f"{' ' * tab}\x1b[2m{data}\x1b[0m"
### examples ###
object = {
"please": "display",
"the": "following",
"containing": [
"lists",
"with",
{
"weird": "a$$",
"elements": None
},
"and",
"even",
"nums:",
12,
3.5,
]
}
print(repr_json(object))
# Note: objects of other types will be displayed as their __repr__ output.
# discord integration
# require a send_to_discord function
send_to_discord(f"```ansi\n{repr_json(object)}\n```")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment