Last active
February 14, 2021 01:35
-
-
Save weirane/119b9e0d73d688bd9151b40789cbaabe to your computer and use it in GitHub Desktop.
Convert exported facebook JSON data to UTF-8 encoded
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
#!/usr/bin/python3 | |
# Convert all JSON files inside directory `facebook`: | |
# $ fd --extension=json . facebook -x ./facebook-convert.py | |
import json | |
import sys | |
def parse(obj): | |
if isinstance(obj, str): | |
return obj.encode('latin_1').decode('utf-8') | |
elif isinstance(obj, dict): | |
return {k: parse(v) for k, v in obj.items()} | |
elif isinstance(obj, list): | |
return [parse(x) for x in obj] | |
else: | |
return obj | |
if __name__ == '__main__': | |
fin = sys.argv[1] | |
orig = json.loads(open(fin).read()) | |
parsed = parse(orig) | |
pp = json.dumps(parsed, indent=2, ensure_ascii=False) | |
open(fin, 'wb').write(pp.encode('utf-8')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment