【问题标题】:JSON manipulation for substituting values python用于替换值python的JSON操作
【发布时间】:2020-09-06 16:35:56
【问题描述】:

我有一个场景,我通过操作 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"
    }
  ]
}

现在这里基于如果data1的eid等于eqiddata2的条件 然后将这两个键的tgssid 的有效负载data1 的值替换为键的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"
   }
]}

任何帮助都会很棒!

【问题讨论】:

  • 如果你得到for k in data1,那么你应该使用k isdie循环。或者你应该得到for k in data1["data"]:并检查k["eid"] != ...添加替换k['sid'] = test['sid']。而这一切都没有append()

标签: python json python-3.x python-2.7


【解决方案1】:

它不是最佳的,但它有效。而且对初学者来说更易读。

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)

对于更大的数据,最好先使用循环创建结构,仅使用来自data2 的值contentauth,然后使用循环在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)

【讨论】:

    【解决方案2】:

    试试这个

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-08
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-16
      相关资源
      最近更新 更多