Skip to content

Commit 3b5ef8f

Browse files
authored
Merge pull request #36 from alingse/add-feat-array-1
try support json array
2 parents 16bf44e + 54f6f47 commit 3b5ef8f

File tree

8 files changed

+60
-41
lines changed

8 files changed

+60
-41
lines changed

Diff for: .travis.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ python:
55
- "2.7"
66
- "3.5"
77
- "3.6"
8+
- "3.7"
89
install:
910
- pip install .
1011
- pip install flake8
@@ -17,7 +18,9 @@ script:
1718
- cat fixture/files/raw.0.json|jsoncsv -e > fixture/files/tmp.expand.0.json
1819
- cat fixture/files/raw.0.json|jsoncsv -e|jsoncsv -r|jsoncsv > fixture/files/tmp.expand.0.json
1920
- cat fixture/files/expand.2.json|jsoncsv|mkexcel -t xls > fixture/files/tmp.output.2.xls
20-
- cat fixture/files/expand.2.json|jsoncsv|mkexcel -t csv > fixture/files/tmp.output.2..csv
21+
- cat fixture/files/expand.2.json|jsoncsv|mkexcel -t csv > fixture/files/tmp.output.2.csv
22+
- cat fixture/files/raw.1.json|jsoncsv -A|mkexcel > fixture/files/tmp.output.1.csv
23+
- cat fixture/files/raw.1.json|jsoncsv --array|mkexcel > fixture/files/tmp.output.1.csv
2124
after_success:
2225
- coveralls
2326
notifications:

Diff for: ChangeLog.md

-29
This file was deleted.

Diff for: fixture/files/raw.1.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"a":{"b":1}}, {"a":{"c":2}}]

Diff for: jsoncsv/jsontool.py

+20-4
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,33 @@ def restore(expobj, separator='.', safe=False):
134134
return origin
135135

136136

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

141-
for line in fin:
142-
obj = json.loads(line)
141+
# default: read json objects from each line
142+
def gen_objs():
143+
for line in fin:
144+
obj = json.loads(line)
145+
yield obj
146+
147+
objs = gen_objs()
148+
149+
if json_array:
150+
# read all input as json array
151+
def gen_objs_from_array():
152+
objs = json.load(fin)
153+
assert isinstance(objs, list)
154+
for obj in objs:
155+
yield obj
156+
157+
objs = gen_objs_from_array()
158+
159+
for obj in objs:
143160
new = func(obj, separator=separator, safe=safe)
144161
content = json.dumps(new, ensure_ascii=False)
145162
if PY2:
146163
fout.write(content.encode('utf-8'))
147164
else:
148165
fout.write(content)
149-
150166
fout.write(str('\n'))

Diff for: jsoncsv/main.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111

1212

1313
@click.command()
14+
@click.option(
15+
'-A',
16+
'--array',
17+
'json_array',
18+
is_flag=True,
19+
default=False,
20+
help='read input file as json array')
1421
@click.option(
1522
'-s',
1623
'--sep',
@@ -42,15 +49,15 @@
4249
'output',
4350
type=click.File('w'),
4451
default=sys.stdout)
45-
def jsoncsv(output, input, expand, restore, safe, separator):
52+
def jsoncsv(output, input, expand, restore, safe, separator, json_array):
4653
if expand and restore:
4754
raise click.UsageError('can not choose both, default is `-e`')
4855

4956
func = jsontool.expand
5057
if restore:
5158
func = jsontool.restore
5259

53-
convert_json(input, output, func, separator, safe)
60+
convert_json(input, output, func, separator=separator, safe=safe, json_array=json_array)
5461

5562
input.close()
5663
output.close()

Diff for: setup.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
setup(
1414
name='jsoncsv',
15-
version='2.2.1',
15+
version='2.2.2',
1616
url='https://github.com/alingse/jsoncsv',
1717
description='A command tool easily convert json file to csv or xlsx.',
1818
long_description=readme,
@@ -28,6 +28,7 @@
2828
'Programming Language :: Python :: 3',
2929
'Programming Language :: Python :: 3.5',
3030
'Programming Language :: Python :: 3.6',
31+
'Programming Language :: Python :: 3.7',
3132
],
3233
install_requires=[
3334
'unicodecsv',
@@ -42,11 +43,12 @@
4243
},
4344
keywords=[
4445
'jsontocsv',
46+
'json2csv',
4547
'jsoncsv',
48+
'command',
49+
'convert',
4650
'json',
4751
'csv',
48-
'xls'
49-
'convert',
50-
'command',
52+
'xls',
5153
],
5254
)

Diff for: tests/test_jsoncsv.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@ def test_jsoncsv_expand(self):
1414
runner = CliRunner()
1515
args = ['-e', 'fixture/files/raw.0.json', 'fixture/files/tmp.expand.0.json']
1616
result = runner.invoke(jsoncsv, args=args)
17-
print(result)
17+
assert result.exit_code == 0
18+
19+
def test_jsoncsv_expand_with_json_array(self):
20+
runner = CliRunner()
21+
args = ['-e', 'fixture/files/raw.1.json', 'fixture/files/tmp.expand.1.json', '-A']
22+
result = runner.invoke(jsoncsv, args=args)
1823
assert result.exit_code == 0

Diff for: tests/test_jsontool.py

+14
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,17 @@ def test_convert_restore(self):
120120

121121
fin.close()
122122
fout.close()
123+
124+
def test_convert_expand_json_array(self):
125+
fin = io.StringIO('[{"a":{"b":3}},{"a":{"c":4}}]')
126+
if PY2:
127+
fout = io.BytesIO()
128+
else:
129+
fout = io.StringIO()
130+
131+
convert_json(fin, fout, expand, json_array=True)
132+
133+
self.assertEqual('{"a.b": 3}\n{"a.c": 4}\n', fout.getvalue())
134+
135+
fin.close()
136+
fout.close()

0 commit comments

Comments
 (0)