在python中根据ID值映射两个json
我有两个 json 如下: json1:这是我提交给 API 的有效负载
{
"id": 0,
"object_type": "SCHEMA",
"object_meta": {
"source_id": 1002,
"database": "raw_exadata",
"schema": "clv"
},
"business_name": "",
"business_logic": "",
"verified_use_cases": ["p"],
"classifications": ["bb464f04-f879-361c-95e5-42bcca6a9535"]
}
,
{
"id": 1,
"object_type": "TABLE",
"object_meta": {
"source_id": 1002,
"database": "raw",
"schema": "clv",
"table": "clv_cust"
},
"business_name": "",
"business_logic": "",
"verified_use_cases": [],
"classifications": ["p"],
}
]
json2:这是 API 调用的响应,它告诉我发布的数据中的错误
{
"errors": {
"0": {
"verified_use_cases": {
"0": [
"Not a valid UUID."
]
}
},
"1": {
"classifications": {
"0": [
"Not a valid UUID."
]
}
}
}
}
我想对发布的有效负载生成一个非常用户友好的响应,说明这样那样的内容对象有无效条目,因此失败。
对于上述情况,我需要将“id”从 json2 映射(或查找)到 json1 并获取 object_type,并且 verify_use_cases 中的条目、分类与相应的 id 是错误的。
到目前为止我已经尝试过:
from collections import defaultdict
p=json.loads(response.text)
result=defaultdict(list)
for i in p['errors']:
result['obj_id'].append(int(i))
result['error'].append(p['errors'][i])
现在这给了我 object_id,但我不确定如何使用 json1 进一步映射它。
请帮忙。
PS:字段、已验证的用例、分类实际上都是用逗号分隔的多值,因此您可以看到错误 json 中的“0”
提前感谢
I have two jsons as follows:
json1:This is the payload I am submitting to an API
{
"id": 0,
"object_type": "SCHEMA",
"object_meta": {
"source_id": 1002,
"database": "raw_exadata",
"schema": "clv"
},
"business_name": "",
"business_logic": "",
"verified_use_cases": ["p"],
"classifications": ["bb464f04-f879-361c-95e5-42bcca6a9535"]
}
,
{
"id": 1,
"object_type": "TABLE",
"object_meta": {
"source_id": 1002,
"database": "raw",
"schema": "clv",
"table": "clv_cust"
},
"business_name": "",
"business_logic": "",
"verified_use_cases": [],
"classifications": ["p"],
}
]
json2: this is the response of an API call which tells me the error in the data that was posted
{
"errors": {
"0": {
"verified_use_cases": {
"0": [
"Not a valid UUID."
]
}
},
"1": {
"classifications": {
"0": [
"Not a valid UUID."
]
}
}
}
}
I want to produce a very user friendly response to the posted payload stating such and such an object has an invalid entry hence it has failed.
For the above, I need to map (or lookup) on the "id" from json2 to json1 and fetch the object_type and also that the entry in the verified_use_cases, classifications is wrong with corresponding id.
What I have tried so far:
from collections import defaultdict
p=json.loads(response.text)
result=defaultdict(list)
for i in p['errors']:
result['obj_id'].append(int(i))
result['error'].append(p['errors'][i])
Now this gives me the object_id, but I am not sure how I can map it further with json1.
Kindly help.
PS: the fields, verified use cases, classifications are all multi valued with comma separated actually, hence you can see the "0" int the error json
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
回复
Response
您应该考虑以下几点:
Pydantic
及其验证器和最简单的转换方法任何 json 到必要的类。因此,当您想创建任何这些已定义的类时,您可以执行以下操作:
示例:
嗯,这是我将如何执行此操作:
You should consider a couple of things in mind:
Pydantic
with it's validators and easiest ways of converting any json to necessary class.pydantic
with your code, example will be below.And as a result when you'd like to create any of these defined classes you can do:
Example:
Hm here's the how would I do this: