1

The output of the openssl command looks like this:

serial=CABCSDUMMY4A168847703FGH
notAfter=Oct 21 16:43:47 2024 GMT
subject= /C=US/ST=WA/L=Seattle/O=MyCo/OU=TME/CN=MyCo.example.com

How do I convert this string to JSON?

I tried these:

temp_txt_bytes = subprocess.check_output (["openssl", "x509", "-serial", "-enddate", "-subject", "-noout", "-in", pem_file_name])

temp_txt_strings = temp_txt_bytes.decode("utf-8")

test = json.loads(temp_txt_strings) #json.parse, json.dump, and json.load also failing
slm
  • 15,396
  • 12
  • 109
  • 124
GManika
  • 505
  • 1
  • 6
  • 13

1 Answers1

2

You can split every line with "=" as a separator, put the two parts in an ordered dictionary and then dump it to json:

my_list = "serial=CABCSDUMMY4A168847703FGH".split("=")
ordered_dict = OrderedDict()
ordered_dict[my_list[0]] = my_list[1]
print(json.dumps(ordered_dict))

the output would be like this:

{"serial": "CABCSDUMMY4A168847703FGH"}

you can do it for all lines. PS don't forget to import json and OrderedDict

zguesmi
  • 321
  • 1
  • 11