用于替换python值的JSON操作

2024-04-28 02:33:02 发布

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

我有一个场景,通过操作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等于eqiddata2 然后为这两个键的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"
   }
]}

任何帮助都会很好


Tags: nameidservicecontentclasscklastsps
2条回答

它不是最优的,但它是有效的。对于初学者来说,它更具可读性

for item1 in data1['data']:
    #print("item1['eid']: ", item1['eid'])

    for item2 in data2:

        if item1['eid'] == item2['eqid']:
            #print("item2['eqid']:", item2['eqid'])

            item1['sid'] = item2['auth']

            #c = []
            #for tag in item2['tags']:
            #    #print(tag['content'])
            #    c.append(tag['content'])
            #item1['tgs'] = ','.join(c)
            item1['tgs'] = ','.join(tag['content'] for tag in item2['tags'])

print(data1)

对于更大的数据,最好先使用loop来创建结构,只使用来自data2的值contentauth,然后使用loop在data1中替换它。这样它将运行更少的循环


完整的工作示例

data2 = [
  {
    "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
  }
]

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"
    }
  ]
}

for item1 in data1['data']:
    print("item1['eid']: ", item1['eid'])
    for item2 in data2:
        if item1['eid'] == item2['eqid']:
            print("item2['eqid']:", item2['eqid'])
            item1['sid'] = item2['auth']
            c = []
            for tag in item2['tags']:
                print(tag['content'])
                c.append(tag['content'])
            c = ','.join(c)
            item1['tgs'] = c

print(data1)

试试这个

pool = {}
for d2 in data2:
    tags = d2["tags"]
    content = [i["content"] for i in tags]
    pool[d2["eqid"]] = [",".join(content), d2["auth"]]
#print(pool)
for i, d1 in enumerate(data1["data"]):
    if(d1["eid"] in pool):
        data1["data"][i]["tgs"] = pool[d1["eid"]][0]
        data1["data"][i]["sid"] = pool[d1["eid"]][1]

print(data1)

相关问题 更多 >