[英]Unable to serialize json array object returned from rest api in python
I am getting a rest api response from google cloud in json format我收到来自谷歌云的 json 格式的 rest api 响应
I assign this response in a python script(3.7) to a dictionary object row={} for inserting to bigquery我在 python 脚本(3.7)中将此响应分配给字典对象 row={} 以插入到 bigquery
"upgrade": {
"distribution": {
"cpeUri": "cpe:/o:centos:centos:7",
"classification": "Unknown",
"severity": "Unknown"
"cve": [
"CVE-2020-3288"
]
}
}
i assign these to the dictionary objects like this:我将这些分配给字典对象,如下所示:
row["cpeUri"]=upgrade.distribution.cpeUri
row["cve"]=upgrade.distribution.cve //**This line gives me the following error:**
root : INFO CVE List ['CVE-2020-3288']
root : ERROR **Object of type Repeated is not JSON serializable**
Traceback (most recent call last):
File "compliance_report.py", line 409, in main
publish_bigquery(occurrenceslist,projectid,batch_id,batch_start_time)
File "compliance_report _latest.py", line 165, in publish_bigquery
row["cve"]=json.dumps(occurrenceresponse.upgrade.distribution.cve)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Repeated is not JSON serializable
root : ERROR
------------------------------\Exception
if the change the code to如果将代码更改为
row["cve"]=json.dumps(upgrade.distribution.cve) row["cve"]=json.dumps(upgrade.distribution.cve)
I am still getting the same error.我仍然遇到同样的错误。
All the other fields are working.所有其他领域都在运作。 Can anyone please suggest a solution?
任何人都可以提出解决方案吗?
The following changes has worked for me:以下更改对我有用:
cve_list = []
for obj in occurrenceresponse.upgrade.distribution.cve:
cve_list.append(obj)
row["cve"]=cve_list
Now I dont get serialization error现在我没有收到序列化错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.