Skip to content

Commit 24da921

Browse files
[io] feat: Add jsonl output option for JsonWriter
1 parent de30866 commit 24da921

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

libs/garf_io/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Currently it supports writing data to the following destination:
1111
|------------| ---------------- | -------- |
1212
| `console` | ConsoleWriter | `page-size=10`,`format=table\|json`|
1313
| `csv` | CsvWriter | `destination-folder` |
14-
| `json` | JsonWriter | `destination-folder` |
14+
| `json` | JsonWriter | `destination-folder`,`format=json\|jsonl`|
1515
| `bq` | BigQueryWriter | `project`, `dataset`, `location`, `write-disposition` |
1616
| `sqldb` | SqlAlchemyWriter | `connection-string`, `if-exists=fail\|replace\|append` |
1717
| `sheets` | SheetsWriter | `share-with`, `credentials-file`, `spreadsheet-url`, `is_append=True\|False`|

libs/garf_io/garf_io/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
__version__ = '0.0.5'
15+
__version__ = '0.0.6'

libs/garf_io/garf_io/writers/json_writer.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
from __future__ import annotations
1717

18-
import json
1918
import logging
2019
import os
20+
from typing import Literal
2121

2222
import smart_open
2323

@@ -34,15 +34,20 @@ class JsonWriter(file_writer.FileWriter):
3434
"""
3535

3636
def __init__(
37-
self, destination_folder: str | os.PathLike = os.getcwd(), **kwargs: str
37+
self,
38+
destination_folder: str | os.PathLike = os.getcwd(),
39+
format: Literal['json', 'jsonl'] = 'json',
40+
**kwargs: str,
3841
) -> None:
3942
"""Initializes JsonWriter based on a destination_folder.
4043
4144
Args:
4245
destination_folder: A local folder where JSON files are stored.
46+
format: Format of json file ('json', 'jsonl').
4347
kwargs: Optional keyword arguments to initialize writer.
4448
"""
4549
super().__init__(destination_folder=destination_folder, **kwargs)
50+
self.format = format
4651

4752
def write(self, report: garf_report.GarfReport, destination: str) -> str:
4853
"""Writes Garf report to a JSON file.
@@ -55,11 +60,14 @@ def write(self, report: garf_report.GarfReport, destination: str) -> str:
5560
Base filename where data are written.
5661
"""
5762
report = self.format_for_write(report)
58-
destination = formatter.format_extension(destination, new_extension='.json')
63+
file_extension = '.json' if self.format == 'json' else '.jsonl'
64+
destination = formatter.format_extension(
65+
destination, new_extension=file_extension
66+
)
5967
self.create_dir()
6068
logging.debug('Writing %d rows of data to %s', len(report), destination)
6169
output_path = os.path.join(self.destination_folder, destination)
6270
with smart_open.open(output_path, 'w', encoding='utf-8') as f:
63-
json.dump(report.to_list(row_type='dict'), f)
71+
f.write(report.to_json(output=self.format))
6472
logging.debug('Writing to %s is completed', output_path)
6573
return f'[JSON] - at {output_path}'

libs/garf_io/tests/unit/writers/test_json_writer.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,25 @@ def test_write_single_column_report_returns_correct_data(
4848

4949
assert data == expected
5050

51+
def test_write_single_column_report_returns_correct_data(
52+
self, single_column_data, output_folder
53+
):
54+
writer = json_writer.JsonWriter(output_folder, format='jsonl')
55+
output = output_folder / 'test.jsonl'
56+
expected = [
57+
{'column_1': 1},
58+
{'column_1': 2},
59+
{'column_1': 3},
60+
]
61+
62+
writer.write(single_column_data, output)
63+
64+
with open(output, 'r') as f:
65+
data = list(f)
66+
67+
for i, json_str in enumerate(data):
68+
assert json.loads(json_str) == expected[i]
69+
5170
def test_write_multi_column_report_returns_correct_data(
5271
self, json_writer, sample_data, output_folder
5372
):

0 commit comments

Comments
 (0)