Added a recursive JSON explorer to remove empty or nulled keys from a dictionary

This commit is contained in:
Dionysus 2024-03-05 22:15:55 -05:00
parent 4cf976aada
commit ed547a27f4
Signed by: acidvegas
GPG Key ID: EF4B922DB85DC9DE
2 changed files with 31 additions and 2 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
# Elasticsearch Recon Ingestion Scripts (ERIS) - Developed by Acidvegas (https://git.acid.vegas/eris) # Elasticsearch Recon Ingestion Scripts (ERIS) - Developed by Acidvegas (https://git.acid.vegas/eris)
# eris.py [asyncronous developement] # eris.py
import asyncio import asyncio
import argparse import argparse

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
# Elasticsearch Recon Ingestion Scripts (ERIS) - Developed by Acidvegas (https://git.acid.vegas/eris) # Elasticsearch Recon Ingestion Scripts (ERIS) - Developed by Acidvegas (https://git.acid.vegas/eris)
# ingest_certstream.py # ingest_certs.py
import asyncio import asyncio
import json import json
@ -123,6 +123,35 @@ async def process_data(file_path: str = None):
await asyncio.sleep(10) await asyncio.sleep(10)
async def strip_struct_empty(data: dict) -> dict:
'''
Recursively remove empty values from a nested dictionary or list.
:param data: The dictionary or list to clean.
'''
empties = [None, '', [], {}]
if isinstance(data, dict):
for key, value in list(data.items()):
if value in empties:
del data[key]
else:
cleaned_value = strip_struct_empty(value)
if cleaned_value in empties:
del data[key]
else:
data[key] = cleaned_value
return data
elif isinstance(data, list):
return [strip_struct_empty(item) for item in data if item not in empties and strip_struct_empty(item) not in empties]
else:
return data
''' '''
Example record: Example record: