Я пытаюсь преобразовать запрос pymssql в json, где первый столбец «значение» будет «ключ», а соответствующие столбцы будут список «ключ: значение».
Я пытался использовать jsondumps, но получитьОшибка «Курсор не JSON-сериализуемый»:
`conn = pymssql.connect(server, port, db)
cursor = conn.cursor('select u_business_service_display_value as Business, name, host_name as hostname, install_status, ip_address, used_for from cmdb_ci_server where u_patching_director_display_value = <Name> AND install_status <> "Retired" AND install_status <> "Pending Retirement" order by Business, hostname')
for row in cursor:
print("Business=%s, Name=%s, Hostname=%s, install_status=%s, ip_address=%s, used_for=%s" % (row['Business'], row['name'], row['hostname'], row['install_status'], row['ip_address'], row['used_for']))
print json.dumps(results, indent=1)
conn.close()`
Вывод
- Business=AAA, Name=Value, Hostname=vaule, install_status=Retired, ip_address=<ip>, used_for=None
- Business=AAA, Name=Value, Hostname=vaule, install_status=Retired, ip_address=<ip>, used_for=None
- Business=BBB, Name=Value, Hostname=vaule, install_status=Installed, ip_address=<ip>, used_for=Prod
- Business=BBB, Name=Value, Hostname=vaule, install_status=Installed, ip_address=<ip>, used_for=Prod
Ожидаемый вывод
{
"AAA":[
{
"Hostname":"Value",
"install_status":"Retired",
"ip_address":"<ip>",
"used_for":"None"
},
{
"Hostname":"Value",
"install_status":"Retired",
"ip_address":"<ip>",
"used_for":"None"
}
],
"BBB":[
{
"Hostname":"Value",
"install_status":"Installed",
"ip_address":"<ip>",
"used_for":"Prod"
},
{
"Hostname":"Value",
"install_status":"Installed",
"ip_address":"<ip>",
"used_for":"Prod"
}
]
}```