Created
November 23, 2018 05:17
-
-
Save haridas/33175b1ebcd9955893a124393c694210 to your computer and use it in GitHub Desktop.
Ensure a big jsons particular field does't includes null, Helpful as part of datacleanup process.
This file contains hidden or 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 | |
def read_json_lines(fname, filed_name): | |
num = 0 | |
doc_size = [] | |
error_docs = [] | |
with open(fname) as f: | |
while True: | |
line = f.readline() | |
if not line: | |
break | |
print (num) | |
num += 1 | |
d = json.loads(line) | |
try: | |
doc_size.append(len(d[field_name].split())) | |
except Exception as ex: | |
error_docs.append(d) | |
return doc_size, error_docs | |
doc_size, error_docs = read_json_lines("./file.json", "data") | |
# Quick analysis of doc size, when the pd.read_json / pd.read_csv fails to read the orginal file. | |
# Fix the error_docs by removing or updating it. | |
doc_df = pd.DataFrame([doc_size]) | |
doc_df.describe() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment