Skip to content

Instantly share code, notes, and snippets.

@garywill
Last active March 29, 2023 08:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garywill/a4547c0cbb4f67473621027416b98200 to your computer and use it in GitHub Desktop.
Save garywill/a4547c0cbb4f67473621027416b98200 to your computer and use it in GitHub Desktop.
convert .har (Firefox browser log) to csv
#!/usr/bin/python3
import json
import csv
json_file_text = ""
with open('INPUTHARJSONFILE') as file:
json_file_text = file.read()
j_obj = json.loads(json_file_text)
es = j_obj ['log'] ['entries']
new_j = []
for i in range(len(es)) :
et = es[i]
new_e = {}
new_e ['t'] = et ['startedDateTime']
new_e ['status'] = str(et ['response'] ['status']) + ' ' + et['response']['statusText']
new_e ['url'] = et ['request'] ['url']
new_e ['method'] = et ['request'] ['method']
new_e ['protocol'] = new_e ['url'] .split('://') [0]
new_e ['domain'] = new_e ['url'] .split('://') [1] .split('/')[0]
new_e ['file'] = new_e ['url'] .split('://') [1] .split('/')[-1] .split('?')[0]
new_e ['file_sfx'] = new_e ['file'] .split('.')[-1]
new_e ['minetype'] = et ['response'] ['content'] ['mimeType'] if et ['response'] ['content'] else ''
new_e ['bodysize'] = et ['response'] ['bodySize']
new_j . append(new_e)
print(new_j)
keysList = list ( new_j[0].keys() )
print(keysList)
with open("OUTPUTCSVFILE.csv", "w") as csvf:
writer = csv.DictWriter(csvf, fieldnames=keysList)
writer.writeheader()
for i in range(len(new_j)) :
print(i)
print(new_j[i])
writer.writerow(new_j[i])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment