Permalink
Browse files

output is now in json format

  • Loading branch information...
1 parent 10f9ac5 commit 7d2fa35c03df0596f91fa742132a4ef844469dd0 @jnovatnack committed Sep 7, 2011
Showing with 14 additions and 18 deletions.
  1. +0 −3 README
  2. +6 −7 json_grep/jgrep.py
  3. +6 −6 json_grep/test/test_jgrep.py
  4. +2 −2 setup.py
View
3 README
@@ -4,9 +4,6 @@ jnovatnack@gmail.com
JGrep is a simple command line utility for filtering JSON files
-Requirements:
-cjson
-
Example:
jgrep logs.txt -k ^ip -k user
cat logs.txt | jgrep -k ^ip -k user
View
@@ -1,9 +1,9 @@
-import sys
-import re
+import json
import logging
+import re
+import sys
from optparse import OptionParser
-from cjson import decode, encode
log = logging.getLogger('jgrep')
@@ -36,7 +36,7 @@ def jgrep_file(self, fd):
line = line.strip()
fline = self.jgrep(line)
if fline:
- yield fline
+ yield json.dumps(fline)
except:
log.exception('Exception reading line %s' % line)
raise
@@ -51,7 +51,7 @@ def jgrep(self, source_str):
:rtype: dict
:returns: A dictonary of filtered keys
"""
- source = decode(source_str)
+ source = json.loads(source_str)
keys = source.keys()
fline = {}
@@ -60,5 +60,4 @@ def jgrep(self, source_str):
match = regex.search(key)
if match is not None:
fline[key] = source[key]
-
- return fline
+ return fline
@@ -1,8 +1,8 @@
+import json
import os
from tempfile import TemporaryFile
from unittest import TestCase
-from cjson import encode, decode
from json_grep import JSONGrep
class TestJSONGrep(TestCase):
@@ -15,22 +15,22 @@ def test_simple(self):
"""
json_grep = JSONGrep(['user'])
source = {'userID': 5, 'url' : 'http://www.google.com'}
- result = json_grep.jgrep(encode(source))
+ result = json_grep.jgrep(json.dumps(source))
self.assertEquals(result, {'userID': 5})
def test_file_jgrep(self):
"""
Ensures we can jgrep a file
"""
tmp_file = TemporaryFile('w+')
- tmp_file.write('%s\n' % encode({'user' : 'john', 'ip' : '192.168.2.2', 'city' : 'philadelphia'}))
- tmp_file.write('%s\n' % encode({'user' : 'jill', 'ip' : '10.0.0.1', 'city' : 'stockholm'}))
+ tmp_file.write('%s\n' % json.dumps({'user' : 'john', 'ip' : '192.168.2.2', 'city' : 'philadelphia'}))
+ tmp_file.write('%s\n' % json.dumps({'user' : 'jill', 'ip' : '10.0.0.1', 'city' : 'stockholm'}))
tmp_file.seek(0)
expected_output = [
- {'user' : 'john', 'ip' : '192.168.2.2'},
- {'user' : 'jill', 'ip' : '10.0.0.1'}]
+ json.dumps({'user' : 'john', 'ip' : '192.168.2.2'}),
+ json.dumps({'user' : 'jill', 'ip' : '10.0.0.1'})]
json_grep = JSONGrep(['user', 'ip'])
output = [line for line in json_grep.jgrep_file(tmp_file)]
View
@@ -1,9 +1,9 @@
from distutils.core import setup
setup(name = 'jgrep',
- version ='0.2',
+ version ='0.3',
description ='A class for filtering files of json objects',
author ='John Novatnack',
author_email ='jnovatnack@gmail.com',
packages = ['json_grep'],
scripts = ['jgrep.py'])
-
+

0 comments on commit 7d2fa35

Please sign in to comment.