Skip to content

Instantly share code, notes, and snippets.

@soaxelbrooke
Last active May 14, 2018 05:11
Show Gist options
  • Save soaxelbrooke/bdf8d5026fe32b6f6406 to your computer and use it in GitHub Desktop.
Save soaxelbrooke/bdf8d5026fe32b6f6406 to your computer and use it in GitHub Desktop.
Decode mongo JSON in Python... with eval
# It's not pretty, but it get's the job done. JSON is valid Python, so you can eval it,
# provided that you have the proper names in scope, like ObjectId and ISODate
from dateutil import parser
NumberLong = int
ObjectId = str
false = False
true = True
null = None
ISODate = parser.parse
lines = ['{"_id" : ObjectId("5539bae79a29809df4008265"), "created_at" : ISODate("2015-04-24T03:39:19.058Z")}',
'{"_id" : ObjectId("5539bae79a29809df4001953"), "created_at" : ISODate("2015-04-24T03:40:01.042Z")}']
docs = [eval(line) for line in lines]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment