Created
August 9, 2024 18:42
-
-
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
This file contains 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 | |
# 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