Skip to content

Instantly share code, notes, and snippets.

@gwillem
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gwillem/8e5c786e91815ab171c8 to your computer and use it in GitHub Desktop.
Save gwillem/8e5c786e91815ab171c8 to your computer and use it in GitHub Desktop.
Smart JSON line parser (for logs etc)
#!/usr/bin/env python
"""
Usage:
$ grep '"status":"200"' /var/log/nginx/access.log | parse-json-lines status,referer
200 http://www.forestryforum.com/board/index.php?PHPSESSID=4db861b1cb6e82ea8ce8284d16465bfa&thememode=mobile;redirect=http%3A%2F%2Fuwall.tv%2Fredir.php%3Fw%3D800%26url%3Dhttp%3A%2F%2Fwww.kookshopdepepermolen.nl%2Fblog%2Fperfecte-kopje-thee-solis-waterkoker%2F
200 http://eduraft.com/common/login/registeruser.php?redirecturl=http://www.ccjpoz.org/libraries/main/exlink.php?url=www.kookshopdepepermolen.nl/blog/perfecte-kopje-thee-solis-waterkoker/
200 http://cloud.itsc.cuhk.edu.hk/redirection/redirect.aspx?URL=http://open-search.eu/recent.php?lang=at&q=www.kookshopdepepermolen.nl/blog/perfecte-kopje-thee-solis-waterkoker/
200 http://www.trade.gov/build/fragments/fl_tg_outsidelinks/redirect.asp?URL=http://1.xg4ken.com/media/redir.php?prof&camp&affcode&cid&networkType=search&url%5B%5D=http%3A//www.kookshopdepepermolen.nl/blog/perfecte-kopje-thee-solis-waterkoker/
200 http://www.swwwc.com/BrentV76luppuzgwxc?likeMode=dislike&url=http://www.ldu.ru/go.php?url=http://www.kookshopdepepermolen.nl/blog/perfecte-kopje-thee-solis-waterkoker/
"""
import json
import fileinput
import sys
import re
try:
arguments = ''.join(sys.argv[1:])
fields = arguments.split(',')
except IndexError:
print "No fields given"
sys.exit(1)
# http://stackoverflow.com/questions/1450393/how-do-you-read-from-stdin-in-python
for line in fileinput.input('-'):
try:
blob = json.loads(line)
except ValueError as e:
# pass 2, it's expensive so only in case of exception
line = line.replace('\\x', '\\u00').strip()
if not line:
continue
try:
blob = json.loads(line)
except ValueError as e:
print "Couldn't parse this line."
print line
print e.message
sys.exit(1)
for f in fields:
try:
print blob[f],
except KeyError as e:
print "This key was not found in the json string:", e.message
print "Pick one of: "
print '\n'.join(["\t" + k for k in blob.keys()])
sys.exit(1)
except IOError:
sys.exit(1)
print "\n",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment