Skip to content

Instantly share code, notes, and snippets.

@Neutrollized
Created March 25, 2025 20:30
Show Gist options
  • Save Neutrollized/0593da8b08775e44d1ebf880ded45247 to your computer and use it in GitHub Desktop.
Save Neutrollized/0593da8b08775e44d1ebf880ded45247 to your computer and use it in GitHub Desktop.
Convert input JSON to NDJSON (newline delimited JSON)
#! /usr/bin/env python3
import json
import sys
def json_to_ndjson(input_file: str, output_file=None):
"""Converts a JSON input file of JSON objects to NDJSON format.
Args:
input_file: A JSON file of JSON objects.
output_file: (Optional) The path to the output NDJSON file. If None,
the NDJSON string is returned.
Returns:
If output_file is None, returns the NDJSON string. Otherwise, returns None (and creates output file)
"""
try:
with open(input_file, 'r') as f:
json_data = json.load(f)
except FileNotFoundError:
print(f"Error: File not found at {input_file}")
sys.exit(1)
except json.JSONDecodeError:
print(f"Error: Invalid JSON format in {input_file}")
sys.exit(2)
except Exception as e:
print(f"An unexpected error occurred: {e}")
sys.exit(10)
if isinstance(json_data, list):
ndjson_lines = [json.dumps(item) for item in json_data]
elif isinstance(json_data, dict):
ndjson_lines = [json.dumps(json_data)]
else:
raise ValueError("Input must be a JSON object (dict) or a list of JSON objects.")
ndjson_string = "\n".join(ndjson_lines)
if output_file:
with open(output_file, "w") as f:
f.write(ndjson_string)
return None
else:
print(ndjson_string)
return ndjson_string
if __name__ == "__main__":
if len(sys.argv) == 2:
input_file = sys.argv[1]
json_to_ndjson(input_file)
elif len(sys.argv) == 3:
input_file = sys.argv[1]
output_file = sys.argv[2]
json_to_ndjson(input_file, output_file)
else:
print("Usage: json_to_ndjson INPUT_FILE [OUTPUT_FILE]")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment