Skip to content

Instantly share code, notes, and snippets.

@rozap
Created October 7, 2016 17:14
Show Gist options
  • Save rozap/eb6e8e603615a5322ca146bfa214425a to your computer and use it in GitHub Desktop.
Save rozap/eb6e8e603615a5322ca146bfa214425a to your computer and use it in GitHub Desktop.
parse ldjson
#!/usr/bin/python
import sys
import json
"""
cat ldjson-thing | ldjson msg,time
"""
def pick(row, path, label = ''):
if len(path) == 1:
[name] = path
return (label + name), row.get(name, 'No field named %s' % name)
name = path[0]
return pick(row.get(name, {}), path[1:], label + name + '.')
if len(sys.argv) < 2:
print "Include an expr!"
sys.exit(1)
suppress_err = (len(sys.argv) == 3) and (sys.argv[2] == '-q')
expr = sys.argv[1]
projections = [path.split('.') for path in expr.split(',')]
for line in sys.stdin:
try:
js = json.loads(line)
picked = [pick(js, path) for path in projections]
print ' '.join(['%s: %s' % (name, value) for (name, value) in picked])
except:
if not suppress_err:
sys.stderr.write("Failed to parse: %s" % line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment