Skip to content

Instantly share code, notes, and snippets.

@wpm
Created December 22, 2017 19:33
Show Gist options
  • Save wpm/0ab2bc3e6b5adf1577a252a08ef3c3c6 to your computer and use it in GitHub Desktop.
Save wpm/0ab2bc3e6b5adf1577a252a08ef3c3c6 to your computer and use it in GitHub Desktop.
Tool to convert a JSON list into a JSONL file.
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