Skip to main content

Python script to parse a PEM certificate into JSON.

#!/usr/bin/python3

##
# Parse a PEM certificate into JSON
# https://stackoverflow.com/a/52196312
#
# Usage: python pem2json.py example.pem
##

import json
import os
import ssl
import sys


def main():
    cert_file_name = os.path.join(os.path.dirname(__file__), sys.argv[1])
    ordered_dict = ssl._ssl._test_decode_cert(cert_file_name)
    print(json.dumps(ordered_dict))


if __name__ == "__main__":
    main()