import json | |
import pandas as pd | |
import sys | |
from tqdm import tqdm | |
import uuid | |
def main(input_file, output_file): | |
# Read JSON data from file | |
with open(input_file, 'r') as f: | |
data = json.load(f) | |
# Process each entry and create a list of dictionaries for DataFrame | |
records = [] | |
for entry in tqdm(data): | |
try: | |
ip = entry['ip'] | |
for port in entry['ports']: | |
# Initialize service fields | |
service_name = None | |
service_banner = None | |
# Extract service information if available | |
if 'service' in port: | |
service = port['service'] | |
service_name = service.get('name') | |
service_banner = service.get('banner') | |
# Create record with consistent fields | |
record = { | |
'ip': str(uuid.uuid4()), | |
'service_name': service_name, | |
'service_banner': service_banner | |
} | |
if service_banner is not None: | |
records.append(record) | |
except KeyError as e: | |
print(f"Keyerror on following entry: `{entry}`... skipping!") | |
pass | |
# Create DataFrame | |
df = pd.DataFrame(records) | |
# Write to Parquet format | |
df.to_parquet(output_file) | |
print(f"Successfully saved Parquet file to {output_file}") | |
if __name__ == "__main__": | |
if len(sys.argv) != 3: | |
print("Usage: python script.py <input.json> <output.parquet>") | |
sys.exit(1) | |
input_json = sys.argv[1] | |
output_parquet = sys.argv[2] | |
main(input_json, output_parquet) | |