Skip to content

Instantly share code, notes, and snippets.

@theonewolf
Created December 10, 2012 20:44
Show Gist options
  • Select an option

  • Save theonewolf/4253245 to your computer and use it in GitHub Desktop.

Select an option

Save theonewolf/4253245 to your computer and use it in GitHub Desktop.
parse a collection of packed JSON documents
#!/usr/bin/env python
import json
data = open('database.json').read()
document = True
start = 0
end = None
while document:
try:
document = json.loads(data[start:end])
print document
if end > 0:
start = end
end = None
# only final document loads and also has end set as None
elif end == None:
break
except ValueError as e:
# first case: if we encountered a true error
if e.args[0] == 'No JSON object could be decoded': break
# second case: find where error occurs, that is end of current document
else: end = start + int(e.args[0].
split('column')[1].strip().
split(' ')[0])
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment