将openssl命令的输出转换为JSON [英] Convert the output of openssl command to JSON

查看:215
本文介绍了将openssl命令的输出转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

openssl命令的输出如下所示:

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

如何将该字符串转换为JSON?

How do I convert this string to JSON?

我尝试了这些:

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

推荐答案

您可以使用"="作为分隔符分割每一行,将这两部分放入有序字典中,然后将其转储到json中:

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))

输出如下:

{"serial": "CABCSDUMMY4A168847703FGH"}

您可以对所有行执行此操作. PS别忘了导入json和OrderedDict

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

这篇关于将openssl命令的输出转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆