Created
December 22, 2017 19:33
-
-
Save wpm/0ab2bc3e6b5adf1577a252a08ef3c3c6 to your computer and use it in GitHub Desktop.
Tool to convert a JSON list into a JSONL file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from json import JSONDecodeError | |
from typing import Sequence | |
import click | |
class JSONList(click.ParamType): | |
def convert(self, value: str, _, __) -> Sequence: | |
try: | |
with open(value) as f: | |
json_list = json.load(f) | |
if not isinstance(json_list, list): | |
self.fail("{value} is not a list of objects.") | |
return json_list | |
except OSError: | |
self.fail("Cannot open {value}.") | |
except JSONDecodeError as e: | |
self.fail("Invalid JSON {e}") | |
@click.command() | |
@click.argument("json_list", type=JSONList()) | |
def json_to_jsonl(json_list): | |
""" | |
Take JSON file that is a list and print it out as a JSONL file. | |
""" | |
for obj in json_list: | |
print(json.dumps(obj)) | |
if __name__ == "__main__": | |
json_to_jsonl() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment