A Python function to convert a JSON file to a JSON Lines file where each item is stored on a separate line.
The use case was that AWS Athena can use JSON data as input, but only if it’s formatted in JSON Lines format. The following Python code achieves exactly this
import json
def convert_json_to_jsonl(filepath_input: str, filepath_output: str) -> None:
"""Converts a JSON file to a JSON line-delimited format, see http://jsonlines.org/"""
with open(filepath_input, "r") as read_file:
data = json.load(read_file)
json_list = [json.dumps(record) for record in data]
with open(filepath_output, 'w') as obj:
for i in json_list:
obj.write(i+'\n')
return None