将JSON值映射到Python中的另一个JSON对象

2024-10-01 02:31:26 发布

您现在位置:Python中文网/ 问答频道 /正文

这个问题的背景是我有JSON响应A,希望只将响应A的值发送到JSON格式B。JSON格式B有不同的字段

JSON响应A

{

"res_comment": "work description",
"res_id": "62",
"res_priority": "P2",
"res_qid": "INC0140315"

} ....... 等x n

希望将JSON响应A中的值映射到下面的JSON格式B

JSON格式B

{

"ServiceIssueCategoryID": "62", **#res_id**
 "ServicePriorityCode": "P2", **#res_priority**
"ServiceNowID_KUT": "INC0140315", **#res_qid**
"ServiceRequestTextCollection":[
{
    "TypeCode": "10004", **value does not change**
    "Text": "work description" **#res_comment**
}
    ]

}

##更新

要明确的是:

我制作了一个值映射(在一组花括号之间)到格式B。然后我使用下面的更新格式B:

with open("sapFormat.json", "r+") as jsonFile: 
    data = json.load(jsonFile) 
    tmp1 = data['ServiceIssueCategoryID'] 
    data["ServiceIssueCategoryID"] = res_id

    .... other fields

工作正常,但是我有100个这样的个人“res_id”。不确定如何逐个循环所有数据并逐个添加。提取值的函数,获取所有值,例如100 x分辨率id,需要获取1个分辨率id,更新B并移动到下一个分辨率id,更新等


Tags: idjsondata格式comment分辨率resdescription