Created
January 6, 2024 22:52
-
-
Save mosajjal/2c1aa9bd8ec58591456459195df54de4 to your computer and use it in GitHub Desktop.
recursively replace base64-encoded strings in a JSON file with the decoded string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# recursively replace base64-encoded strings in a JSON file with the decoded string | |
# usage: python main.py <input_file> <output_file> | |
import sys | |
import json | |
import base64 | |
def decode_json(data): | |
if isinstance(data, dict): | |
return {decode_json(key): decode_json(value) for key, value in data.items()} | |
elif isinstance(data, list): | |
return [decode_json(element) for element in data] | |
elif isinstance(data, str): | |
try: | |
return decode_json(json.loads(base64.b64decode(data))) | |
except: | |
return data | |
else: | |
return data | |
def main(): | |
if len(sys.argv) != 3: | |
print('usage: python main.py <input_file> <output_file>') | |
exit(1) | |
with open(sys.argv[1], 'r') as f: | |
data = json.load(f) | |
with open(sys.argv[2], 'w') as f: | |
json.dump(decode_json(data), f, indent=4) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment