-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScriptForJSON.py
More file actions
24 lines (19 loc) · 809 Bytes
/
Copy pathScriptForJSON.py
File metadata and controls
24 lines (19 loc) · 809 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import json
def clean_json_file(json_file):
with open(json_file, 'r') as file:
data = json.load(file)
# Parse the JSON string into a list of dictionaries if needed
if isinstance(data, str):
data = json.loads(data)
# Iterate through entries and remove 'field2' and entries with null 'field1'
cleaned_data = [entry for entry in data]
print(cleaned_data)
# for entry in cleaned_data:
# entry.pop('field2', None)
# # Write cleaned data back to the file
# with open(json_file, 'w') as file:
# json.dump(cleaned_data, file, indent=4)
# Example usage:
json_file = 'data1.json' # Replace 'example.json' with your JSON file path
clean_json_file(json_file)
print("Data cleaned successfully.")