Skip to content

Commit

Permalink
Merge pull request #36 from alingse/add-feat-array-1
Browse files Browse the repository at this point in the history
try support json array
  • Loading branch information
alingse authored Sep 24, 2019
2 parents 16bf44e + 54f6f47 commit 3b5ef8f
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 41 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ python:
- "2.7"
- "3.5"
- "3.6"
- "3.7"
install:
- pip install .
- pip install flake8
Expand All @@ -17,7 +18,9 @@ script:
- cat fixture/files/raw.0.json|jsoncsv -e > fixture/files/tmp.expand.0.json
- cat fixture/files/raw.0.json|jsoncsv -e|jsoncsv -r|jsoncsv > fixture/files/tmp.expand.0.json
- cat fixture/files/expand.2.json|jsoncsv|mkexcel -t xls > fixture/files/tmp.output.2.xls
- cat fixture/files/expand.2.json|jsoncsv|mkexcel -t csv > fixture/files/tmp.output.2..csv
- cat fixture/files/expand.2.json|jsoncsv|mkexcel -t csv > fixture/files/tmp.output.2.csv
- cat fixture/files/raw.1.json|jsoncsv -A|mkexcel > fixture/files/tmp.output.1.csv
- cat fixture/files/raw.1.json|jsoncsv --array|mkexcel > fixture/files/tmp.output.1.csv
after_success:
- coveralls
notifications:
Expand Down
29 changes: 0 additions & 29 deletions ChangeLog.md

This file was deleted.

1 change: 1 addition & 0 deletions fixture/files/raw.1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"a":{"b":1}}, {"a":{"c":2}}]
24 changes: 20 additions & 4 deletions jsoncsv/jsontool.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,33 @@ def restore(expobj, separator='.', safe=False):
return origin


def convert_json(fin, fout, func, separator=".", safe=False):
def convert_json(fin, fout, func, separator=".", safe=False, json_array=False):
if func not in [expand, restore]:
raise ValueError("unknow convert_json type")

for line in fin:
obj = json.loads(line)
# default: read json objects from each line
def gen_objs():
for line in fin:
obj = json.loads(line)
yield obj

objs = gen_objs()

if json_array:
# read all input as json array
def gen_objs_from_array():
objs = json.load(fin)
assert isinstance(objs, list)
for obj in objs:
yield obj

objs = gen_objs_from_array()

for obj in objs:
new = func(obj, separator=separator, safe=safe)
content = json.dumps(new, ensure_ascii=False)
if PY2:
fout.write(content.encode('utf-8'))
else:
fout.write(content)

fout.write(str('\n'))
11 changes: 9 additions & 2 deletions jsoncsv/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@


@click.command()
@click.option(
'-A',
'--array',
'json_array',
is_flag=True,
default=False,
help='read input file as json array')
@click.option(
'-s',
'--sep',
Expand Down Expand Up @@ -42,15 +49,15 @@
'output',
type=click.File('w'),
default=sys.stdout)
def jsoncsv(output, input, expand, restore, safe, separator):
def jsoncsv(output, input, expand, restore, safe, separator, json_array):
if expand and restore:
raise click.UsageError('can not choose both, default is `-e`')

func = jsontool.expand
if restore:
func = jsontool.restore

convert_json(input, output, func, separator, safe)
convert_json(input, output, func, separator=separator, safe=safe, json_array=json_array)

input.close()
output.close()
Expand Down
10 changes: 6 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name='jsoncsv',
version='2.2.1',
version='2.2.2',
url='https://github.com/alingse/jsoncsv',
description='A command tool easily convert json file to csv or xlsx.',
long_description=readme,
Expand All @@ -28,6 +28,7 @@
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
],
install_requires=[
'unicodecsv',
Expand All @@ -42,11 +43,12 @@
},
keywords=[
'jsontocsv',
'json2csv',
'jsoncsv',
'command',
'convert',
'json',
'csv',
'xls'
'convert',
'command',
'xls',
],
)
7 changes: 6 additions & 1 deletion tests/test_jsoncsv.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@ def test_jsoncsv_expand(self):
runner = CliRunner()
args = ['-e', 'fixture/files/raw.0.json', 'fixture/files/tmp.expand.0.json']
result = runner.invoke(jsoncsv, args=args)
print(result)
assert result.exit_code == 0

def test_jsoncsv_expand_with_json_array(self):
runner = CliRunner()
args = ['-e', 'fixture/files/raw.1.json', 'fixture/files/tmp.expand.1.json', '-A']
result = runner.invoke(jsoncsv, args=args)
assert result.exit_code == 0
14 changes: 14 additions & 0 deletions tests/test_jsontool.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,17 @@ def test_convert_restore(self):

fin.close()
fout.close()

def test_convert_expand_json_array(self):
fin = io.StringIO('[{"a":{"b":3}},{"a":{"c":4}}]')
if PY2:
fout = io.BytesIO()
else:
fout = io.StringIO()

convert_json(fin, fout, expand, json_array=True)

self.assertEqual('{"a.b": 3}\n{"a.c": 4}\n', fout.getvalue())

fin.close()
fout.close()

0 comments on commit 3b5ef8f

Please sign in to comment.