python 比较kwy列表与json密钥并创建新json

xggvc2p6  于 2023-02-18  发布在  Python
关注(0)|答案(1)|浏览(95)

我有密钥列表(我只给出4个作为示例。它可能是100- 1 k密钥)

['session_id','following_status','job_name','last_interaction']

现在,我得到输入json如下,

[
  {
    "updated_data": {
      "last_interaction": "2022-06-20 06:55:55.652434",
      "following_status": "not followed",
      "session_id": "abc1",
      "job_name": "blogger-following",
      "target": "ghjk",
      "liked": 21,
      "watched": 8,
      "commented": 0,
      "followed": false,
      "unfollowed": false,
      "scraped": false,
      "pm_sent": false
    }
  },
  {
    "updated_data": {
      "last_interaction": "2022-06-20 06:55:55.652434",
      "following_status": "followed",
      "session_id": "abc2",
      "job_name": "blogger-following",
      "target": "name2",
      "liked": 1,
      "watched": 7,
      "commented": 0,
      "followed": false,
      "unfollowed": false,
      "scraped": false,
      "pm_sent": false
    }
  },
  {
    "updated_data": {
      "last_interaction": "2022-06-20 06:55:55.652434",
      "following_status": "followed",
      "session_id": "abc3",
      "job_name": "blogger-following",
      "target": "name3",
      "liked": 67,
      "watched": 78,
      "commented": 0,
      "followed": false,
      "unfollowed": false,
      "scraped": false,
      "pm_sent": false
    }
  },
  {
    "updated_data": {
      "last_interaction": "2022-06-20 06:55:55.652434",
      "following_status": "followed",
      "session_id": "abc4",
      "job_name": "blogger-following",
      "target": "name4",
      "liked": 210,
      "watched": 77,
      "commented": 0,
      "followed": false,
      "unfollowed": false,
      "scraped": false,
      "pm_sent": false
    }
  },
  {
    "updated_data": {
      "last_interaction": "2022-06-20 06:55:55.652434",
      "following_status": "followed",
      "session_id": "abc5",
      "job_name": "blogger-following",
      "target": "name5",
      "liked": 21,
      "watched": 790,
      "commented": 0,
      "followed": false,
      "unfollowed": false,
      "scraped": false,
      "pm_sent": false
    }
  },
  {
    "updated_data": {
      "last_interaction": "2022-06-20 06:55:55.652434",
      "following_status": "not followed",
      "session_id": "abc6",
      "job_name": "blogger-following",
      "target": "name6",
      "liked": 81,
      "watched": 7,
      "commented": 0,
      "followed": false,
      "unfollowed": false,
      "scraped": false,
      "pm_sent": false
    }
  }
]

我需要迭代json,如果与上面列表中的键匹配,我需要获取这些键值并推送到新的json文件
我应该通过匹配键来创建JSON,如果不匹配,则忽略那些键值。
输出将是,

[
  {
    "last_interaction": "2022-06-20 06:55:55.652434",
    "following_status": "not followed",
    "session_id": "abc1",
    "job_name": "blogger-following"
    
  },
  {
    "last_interaction": "2022-06-20 06:55:55.652434",
    "following_status": "followed",
    "session_id": "abc2",
    "job_name": "blogger-following"
  },
  {
    "last_interaction": "2022-06-20 06:55:55.652434",
    "following_status": "followed",
    "session_id": "abc3",
    "job_name": "blogger-following"
  },
  {
    "last_interaction": "2022-06-20 06:55:55.652434",
    "following_status": "followed",
    "session_id": "abc4",
    "job_name": "blogger-following"
  },
  {
    "last_interaction": "2022-06-20 06:55:55.652434",
    "following_status": "followed",
    "session_id": "abc5",
    "job_name": "blogger-following"
  },
  {
    "last_interaction": "2022-06-20 06:55:55.652434",
    "following_status": "not followed",
    "session_id": "abc6",
    "job_name": "blogger-following"
  }
]

我所尝试的

import json
from datetime import datetime
from operator import itemgetter

with open("data.json", "r") as f_in:
    data = json.load(f_in)

items = ["session_id", "following_status", "job_name", "last_interaction"]

i = itemgetter(*items)
out = [dict(zip(items, v)) for v in enumerate(data[0]['updated_data'].items())]

我都试过了

[{k: msgs[0]['updated_data'][k] for k in items if k in enumerate(msgs[0]['updated_data'].items())}]

未工作
请告诉我怎样才能达到目标。

nvbavucw

nvbavucw1#

分别应用 * itemgetter * i

keys = ["session_id", "following_status", "job_name", "last_interaction"]
i = itemgetter(*keys)
out = [dict(zip(keys, i(d['updated_data']))) for d in data]
print(out)
[{'following_status': 'not followed',
  'job_name': 'blogger-following',
  'last_interaction': '2022-06-20 06:55:55.652434',
  'session_id': 'abc1'},
 {'following_status': 'followed',
  'job_name': 'blogger-following',
  'last_interaction': '2022-06-20 06:55:55.652434',
  'session_id': 'abc2'},
 {'following_status': 'followed',
  'job_name': 'blogger-following',
  'last_interaction': '2022-06-20 06:55:55.652434',
  'session_id': 'abc3'},
 {'following_status': 'followed',
  'job_name': 'blogger-following',
  'last_interaction': '2022-06-20 06:55:55.652434',
  'session_id': 'abc4'},
 {'following_status': 'followed',
  'job_name': 'blogger-following',
  'last_interaction': '2022-06-20 06:55:55.652434',
  'session_id': 'abc5'},
 {'following_status': 'not followed',
  'job_name': 'blogger-following',
  'last_interaction': '2022-06-20 06:55:55.652434',
  'session_id': 'abc6'}]

相关问题