Created
July 26, 2012 20:21
-
-
Save lukecampbell/3184275 to your computer and use it in GitHub Desktop.
This file contains hidden or 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/env python | |
class Document(object): | |
pass | |
class Library(Document): | |
pass | |
class LifeStage(Document): | |
pass | |
class Diel(Document): | |
pass | |
class Taxis(Document): | |
pass | |
def copy_value(key, item=None): | |
if isinstance(item, list) and len(item): | |
n_list = [ copy_value(key, sub) for sub in item ] | |
elif isinstance(item, dict): | |
# check for a recognized key | |
doc = dict() | |
print 'key is %s' % key | |
if key == "library": | |
print 'found Library' | |
elif key == "lifestages": | |
print 'Lifestages' | |
elif key == "diel": | |
print 'diel' | |
elif key == "taxis": | |
print 'taxis' | |
for skey in item.keys(): | |
doc[skey] = copy_value(skey, item[skey]) | |
if isinstance(doc, Document): | |
print 'Document!?' | |
# doc.save() | |
return doc | |
if __name__ == '__main__': | |
print 'hi' | |
sample_json = '''{ | |
"results": [ | |
{ | |
"status": "private", | |
"_id": { | |
"$oid": "50118ad9963cfb104d000005" | |
}, | |
"name": "dfgh21", | |
"_keywords": [ | |
"dfgh", | |
"fdgh", | |
"dsfg", | |
"fgh", | |
"asdf", | |
"fgh", | |
"xcvb", | |
"qwer", | |
"dsfg", | |
"cvb" | |
], | |
"created": 1343322114503, | |
"geometry": "POLYGON ((-145.9049999999999727 40.9703904651301301, -148.7174999999999727 26.4199509113945510, -132.1940624999999727 21.6048715743020239, -128.6784374999999727 39.0861889844525123, -145.9049999999999727 40.9703904651301301))", | |
"geo_keywords": [ | |
"qwer", | |
"dsfg", | |
"cvb" | |
], | |
"keywords": [ | |
"asdf", | |
"fgh", | |
"xcvb" | |
], | |
"user": "[email protected]", | |
"common_name": "fgh", | |
"genus": "fdgh", | |
"species": "dsfg", | |
"lifestages": [ | |
{ | |
"vss": 234.0, | |
"name": "asdf", | |
"duration": 4356, | |
"diel": [ | |
{ | |
"plus_or_minus": "+", | |
"min": 345.0, | |
"max": 348.0, | |
"hours": 2345, | |
"time": null, | |
"_id": { | |
"$oid": "50118ad9963cfb104d000000" | |
}, | |
"type": "cycles", | |
"cycle": "sunrise" | |
}, | |
{ | |
"plus_or_minus": "+", | |
"min": 345.0, | |
"max": 348.0, | |
"hours": 234, | |
"time": null, | |
"_id": { | |
"$oid": "50118ad9963cfb104d000001" | |
}, | |
"type": "cycles", | |
"cycle": "sunrise" | |
} | |
], | |
"_collection": "libraries", | |
"_database": "fishreport_development", | |
"taxis": [ | |
{ | |
"min": 2345.0, | |
"gradient": 3456.0, | |
"max": 345.0, | |
"variable": "sea_water_temperature", | |
"units": "\u00b0C", | |
"_id": { | |
"$oid": "50118ad9963cfb104d000002" | |
} | |
}, | |
{ | |
"min": 2342.0, | |
"gradient": 34343.0, | |
"max": 345234.0, | |
"variable": "sea_water_temperature", | |
"units": "\u00b0C", | |
"_id": { | |
"$oid": "50118ad9963cfb104d000003" | |
} | |
} | |
], | |
"_id": { | |
"$oid": "50118ad9963cfb104d000004" | |
} | |
} | |
] | |
} | |
] | |
}''' | |
import simplejson as json | |
python_dict = json.loads(sample_json) | |
copy_value(None,python_dict) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: