Skip to content

Instantly share code, notes, and snippets.

@monocongo
Created August 9, 2024 18:42
Show Gist options
  • Save monocongo/c0d68e1d6596cc2b66c1eacf4d0f39f7 to your computer and use it in GitHub Desktop.
Save monocongo/c0d68e1d6596cc2b66c1eacf4d0f39f7 to your computer and use it in GitHub Desktop.
Detect whether a JSON file is using JSON lines or a single JSON object
import json
# try to load the entire file as a single JSON object
file_path = '/home/james/tmp/api_data.json'
try:
with open(file_path, 'r') as f:
data = json.load(f)
print("The file is a single JSON object.")
except json.JSONDecodeError:
print("The file is not a single JSON object.")
# try to load the file line by line
with open(file_path, 'r') as f:
lines = f.readlines()
try:
data = [json.loads(line) for line in lines]
print("The file is a list of JSON lines.")
except json.JSONDecodeError:
print("The file is not a list of JSON lines either.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment