Skip to content

Commit c3f650c

Browse files
committed
Fix fromjson() to support reading from stdin
1 parent 9fe7939 commit c3f650c

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

petl/io/json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from petl.util.base import data, Table, dicts as _dicts, iterpeek
1717

1818

19-
def fromjson(source, *args, **kwargs):
19+
def fromjson(source=None, *args, **kwargs):
2020
"""
2121
Extract data from a JSON file. The file must contain a JSON array as
2222
the top level object, and each member of the array will be treated as a

petl/test/test_executable.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,15 @@ def test_executable():
88
petl 'fromcsv().cut("foo").head(1).tocsv()'
99
""", shell=True, check=True, capture_output=True)
1010
assert result.stdout == b'foo\r\na\r\n'
11+
12+
def test_json_stdin():
13+
result = subprocess.run("""
14+
echo '[{"foo": "a", "bar": "b"}]' |
15+
petl 'fromjson().tocsv()'
16+
""", shell=True, check=True, capture_output=True)
17+
assert result.stdout == b'foo,bar\r\na,b\r\n'
18+
result = subprocess.run("""
19+
( echo '{"foo": "a", "bar": "b"}' ; echo '{"foo": "c", "bar": "d"}' ) |
20+
petl 'fromjson(lines=True).tocsv()'
21+
""", shell=True, check=True, capture_output=True)
22+
assert result.stdout == b'foo,bar\r\na,b\r\nc,d\r\n'

0 commit comments

Comments
 (0)