File size: 1,717 Bytes
e0bef51 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
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)
|