Last active
August 29, 2015 14:01
-
-
Save trajber/35aa16e7293c4397d49a to your computer and use it in GitHub Desktop.
A simple utility to parse JSON files.
This file contains hidden or 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 | |
# -*- coding: utf-8 -*- | |
import json, sys | |
lines=[] | |
for line in sys.stdin: | |
stripped = line.strip() | |
if not stripped: break | |
lines.append(stripped) | |
content=''.join([str(x) for x in lines]) | |
json_content=json.loads(content) | |
for arg in sys.argv[1:]: | |
cont_aux=json_content | |
tokens=arg.split(".") | |
for i in range(len(tokens)): | |
if i==len(tokens)-1: | |
v=cont_aux[tokens[i]] | |
else: | |
cont_aux=cont_aux[tokens[i]] | |
if isinstance(v, list): | |
for j in v: | |
print j.encode('utf-8').strip() | |
elif isinstance(v, basestring): | |
print v.encode('utf-8').strip() | |
else: | |
print str(v).strip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ cat example.json | spy.py example hello.world 1 2