在Python中添加/删除JSON子树
下面是我打算在后端使用的空JSON:在Python中添加/删除JSON子树,python,json,Python,Json,下面是我打算在后端使用的空JSON: { "user": "", "mids": { "merchant_id": { "name": "", "cruise_credentials": { "APIkey": "", "APIident
{
"user": "",
"mids": {
"merchant_id": {
"name": "",
"cruise_credentials": {
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB": {
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
}
}
}
每个用户可能有多个商户ID。因此,我需要能够添加另一个完整的商户ID子树,如下所示:
{
"user": "",
"mids": {
"merchant_id": {
"name": "",
"cruise_credentials": {
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB": {
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
},
"merchant_id2": {
"name": "",
"cruise_credentials": {
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB": {
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
}
}
}
有没有简单的方法可以在Python中添加/删除JSON
非常感谢 您可以尝试.update()
base={
“用户”:“,
“mids”:{
“商户id”:{
“名称”:“,
“邮轮认证”:{
“APIkey”:“,
“apidentifier”:“,
“OrgUnitId”:”
},
“SAWB”:{
“ProfileID”:“,
“AccesKey”:“,
“加密密钥”:”
}
}
}
}
另一个商户={“商户id2”:{
“名称”:“,
“邮轮认证”:{
“APIkey”:“,
“apidentifier”:“,
“OrgUnitId”:”
},
“SAWB”:{
“ProfileID”:“,
“AccesKey”:“,
“加密密钥”:”
}
}
}
基本[“mids”]更新(另一个商户)
打印(基本)
非常感谢你的帮助 有两种解决方案取决于我们如何处理这一问题 使用商户id列表将允许在名字词典之后简单地添加新的词典名称
{
"user": "",
"merchant_id": [
{
"name": "",
"cruise_credentials": {
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB": {
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
},
{
"name": "",
"cruise_credentials": {
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB": {
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
}
]
}
或者我们可以使用.update()函数。但是,这将迫使我们将字段“merchant_name”更改为merchant_name 2,并需要额外的逻辑。
“name”:“IamNumber1”:{
似乎不是valid@Epsi95我在JSON中有一个输入错误,现在已经更正了。这个想法是添加子树merchant_id2(和merchant_id3…4…5…)。如果一个密钥可以有多个值,您可能应该使用列表而不是更改密钥名称。“merchant_id”:[{“name”:“one”},{“name”:“two”}]
{
"user": "",
"merchant_id": [
{
"name": "",
"cruise_credentials": {
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB": {
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
},
{
"name": "",
"cruise_credentials": {
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB": {
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
}
]
}