我有一个场景,通过操作json负载(data2)替换负载(data1)中的值
数据2:
[
{
"eqid": 71430,
"tags": [
{
"id": 135853,
"content": "content1",
"class_id": 13733,
"class_name": "reEs"
},
{
"id": 1358341,
"content": "content2",
"class_id": 13734447,
"class_name": "reEp"
},
{
"id": 135832561,
"content": "content3",
"class_id": 137342347,
"class_name": "reEj"
},
{
"id": 1358234561,
"content": "content4",
"class_id": 137123347,
"class_name": "reEk"
},
{
"id": 1355678561,
"content": "content5",
"class_id": 137432347,
"class_name": "reEm"
},
{
"id": 1352348561,
"content": "content6",
"class_id": 137786347,
"class_name": "reEn"
}
],
"auth": false
},
{
"eqid": 243582,
"tags": [
{
"id": 1358456561,
"content": "content1",
"class_id": 137213347,
"class_name": "reEl"
},
{
"id": 13584567561,
"content": "content2",
"class_id": 13745347,
"class_name": "reEt"
},
{
"id": 1353218561,
"content": "content3",
"class_id": 137980347,
"class_name": "reEf"
},
{
"id": 13589758561,
"content": "content4",
"class_id": 1375678347,
"class_name": "reEb"
}
],
"auth": false
},
{
"eqid": 243672,
"tags": [
{
"id": 1358456561,
"content": "content1",
"class_id": 137213347,
"class_name": "reEl"
},
{
"id": 13589758561,
"content": "content4",
"class_id": 1375678347,
"class_name": "reEb"
}
],
"auth": false
}
]
数据1-
data1 = {
"data": [
{
"name": "app-pp",
"ck_name": "App1",
"eid": 71430,
"Iar": "Osk",
"sps": "Active",
"tgs": "tobedonetages",
"sid": "tobedoneservice",
"last_checked": "19-05-2020"
},
{
"name": "app-pq",
"ck_name": "App2",
"eid": 243582,
"Iar": "Osk",
"sps": "Active",
"tgs": "tobedonetages",
"sid": "tobedoneservice",
"last_checked": "19-05-2020"
}
]
}
现在这里基于这样的条件,如果数据1的eid
等于eqid
data2
然后为这两个键的tgs
替换有效负载的值data1
&sid
,值来自键的content
(标记下)和auth
的data2
我所尝试的:
for tes in data2:
tempmed = tes["eqid"]
tempservice = tes["auth"]
tempservicel = tes["tags"]
for k in data1:
templand= tempkey["name"]
temphck= tempkey["ck_name"]
tempevalid= tempkey["eid"]
tempiaas= tempkey["Iar"]
tempspc= tempkey["sps"]
temptag= tempkey["tas"]
tempserv= tempkey["sid"]
templc = tempkey["last_checked"]
if tempmed == tempevalid:
tempserv = tempservice
temptag = tempservicel
data1.append({'name': templand, 'ck_name': temphck, 'eid': tempevalid, 'Iar': tempiaas, 'sps': tempspc, 'tgs': temptag, 'sid': tempserv, 'last_checked': templc})
我不确定应该用什么方法来实现这一点,因为我目前的方法并不像预期的那样有效
预期O/p:
{"data":[
{
"name":"app-pp",
"ck_name":"App1",
"eid":71430,
"Iar":"Osk",
"sps":"Active",
"tgs":"content1,content2,content3,content4,content5,content6",
"sid":"false",
"last_checked":"19-05-2020"
},
{
"name":"app-pq",
"ck_name":"App2",
"eid":243582,
"Iar":"Osk",
"sps":"Active",
"tgs":"content1,content2,content3,content4",
"sid":"false",
"last_checked":"19-05-2020"
}
]}
任何帮助都会很好
它不是最优的,但它是有效的。对于初学者来说,它更具可读性
对于更大的数据,最好先使用loop来创建结构,只使用来自
data2
的值content
和auth
,然后使用loop在data1
中替换它。这样它将运行更少的循环完整的工作示例
试试这个
相关问题 更多 >
编程相关推荐