Created
November 22, 2016 20:35
-
-
Save Roguelazer/342d44f43bbef7c5c6cbc3de5e474c05 to your computer and use it in GitHub Desktop.
This file contains 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 | |
import collections | |
import json | |
import sys | |
import csv | |
Key = collections.namedtuple('Key', ['path', 'default']) | |
Field = collections.namedtuple('Field', ['label', 'key']) | |
# Mapping from CSV column name to key. Path is either a list of keys or a | |
# dot-delimited string (if there are no dots in your keys). | |
FIELDS = [ | |
Field('timestamp', Key('timestamp', None)), | |
Field('user_id', Key('msg.user.id', None)), | |
Field('mode', Key('msg.mode', None)) | |
] | |
def get_deep(d, k, default): | |
if not isinstance(k, list): | |
k = k.split('.') | |
f = k[0] | |
k = k[1:] | |
if k: | |
if f in d: | |
return get_deep(d[f], k, default) | |
else: | |
return default | |
else: | |
return d.get(f, default) | |
writer = csv.writer(sys.stdout) | |
writer.writerow([f.label for f in FIELDS]) | |
for line in sys.stdin: | |
body = json.loads(line) | |
row = [] | |
for field in FIELDS: | |
row.append(get_deep(body, field.key.path, field.key.default)) | |
writer.writerow(row) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment