Created
September 15, 2021 18:37
-
-
Save kcotten/c283af3d990a194b7211ec630a200fcd to your computer and use it in GitHub Desktop.
Dynamic json in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python3 | |
import json | |
s = """{ | |
"_id": "61410596ba0c39a1fae2f47f", | |
"index": 0, | |
"guid": "fe1102ba-b5f9-4783-a62c-13adb3c2c293", | |
"isActive": false, | |
"balance": "$1,500.31", | |
"picture": "http://placehold.it/32x32", | |
"age": 33, | |
"eyeColor": "blue", | |
"name": "Aguirre Aguilar", | |
"gender": "male", | |
"company": "COMTRAK", | |
"email": "aguirreaguilar@comtrak.com", | |
"phone": "+1 (828) 595-3377", | |
"address": "425 Cheever Place, Norfolk, New York, 6272", | |
"about": "Veniam do incididunt dolor commodo labore ea sint dolore . Nulla amet non Lorem aliquip proident laboris ut. Minim sint ullamco eu magna voluptate. Ut esse cupidatat minim est qui irure ex ullamco qui aliquip.\r\n", | |
"registered": "2019-05-12T10:09:42 +07:00", | |
"latitude": 34.248956, | |
"longitude": 124.1861, | |
"tags": [ | |
"adipisicing", | |
"cillum", | |
"mollit", | |
"consectetur", | |
"adipisicing", | |
"duis", | |
"adipisicing" | |
], | |
"friends": [{ | |
"id": 0, | |
"name": "Susanne Anderson" | |
}, | |
{ | |
"id": 1, | |
"name": "Kara Moon" | |
}, | |
{ | |
"id": 2, | |
"name": "Petty Holden" | |
} | |
], | |
"greeting": "Hello, Aguirre Aguilar! You have 4 unread messages.", | |
"favoriteFruit": "strawberry" | |
}""" | |
def typeToString(obj): | |
t = type(obj) | |
if t is dict: | |
return "Object" | |
elif t is list: | |
return "Array" | |
elif t is str: | |
return "string" | |
elif t is int: | |
return "Int" | |
elif t is float: | |
return "Float" | |
elif t is bool: | |
return "Boolean" | |
else: | |
return "None" | |
# Begin pretty printing | |
def parse(obj): | |
t = typeToString((obj)) | |
print(t) | |
recurse(obj, 1) | |
def recurse(obj, depth=0, key=""): | |
hyphens = "" | |
for i in range(depth*4): | |
hyphens = hyphens + "-" | |
t_obj = typeToString(obj) | |
if t_obj == "Array": | |
for i in obj: | |
# Handle array | |
t = typeToString(i) | |
if t == "Object" or t == "Array": | |
print(hyphens + " " + t) | |
recurse(i, depth + 1) | |
break | |
print(hyphens + " " + key + " " + t) | |
# this is the case where the array is of primitives | |
break | |
else: | |
if obj != None: | |
for k in obj: | |
# print hyphen, plus key, plus type | |
t = typeToString(obj[k]) | |
if t == "Object" or t == "Array": | |
print(hyphens + " " + t) | |
recurse(obj[k], depth + 1, k) | |
continue | |
print(hyphens + " " + k + " " + t) | |
def run(): | |
# read in example json and parse | |
transform = str(s).strip("'<>() ").replace('\'', '\"') | |
obj = json.loads(transform, strict=False) | |
parse(obj) | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment