You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何过滤JSON数据提取多变量?解决Notion JSON的KeyError问题

解决KeyError: 'properties'及邮箱过滤问题

问题背景

需要从Notion导出的JSON数据中过滤出目标邮箱nospam1@gmail.com,运行测试代码时触发KeyError: 'properties'错误。

测试代码

import json

with open('notion_data.json') as json_file:
    data = json.load(json_file)

if [x for x in data['properties'] if x.get('plain_text')=='nospam1@gmail.com']:
  print("IN")
else:
  print("NOT")

报错信息

Traceback (most recent call last):
  File "C:\Users\stijn\PycharmProjects\notion\scrath_notion.py", line 13, in <module>
    if [x for x in data['properties'] if x.get('plain_text')=='nospam1@gmail.com']:
                   ~~~~^^^^^^^^^^^^^^
KeyError: 'properties'

Process finished with exit code 1

Notion导出的JSON结构

{
  "object": "list",
  "results": [
    {
      "object": "page",
      "id": "a94f4f2d-b965-43db-a8bf-02c1453033ee",
      "created_time": "2022-11-15T18:53:00.000Z",
      "last_edited_time": "2022-11-15T18:58:00.000Z",
      "created_by": {
        "object": "user",
        "id": "9b60ada0-dc62-441f-8c0a-e1668a878d0e"
      },
      "last_edited_by": {
        "object": "user",
        "id": "9b60ada0-dc62-441f-8c0a-e1668a878d0e"
      },
      "cover": null,
      "icon": null,
      "parent": {
        "type": "database_id",
        "database_id": "4279b28e-fd9d-4efd-b9f7-957699839dd4"
      },
      "archived": false,
      "properties": {
        "email_sender": {
          "id": "CdJY",
          "type": "rich_text",
          "rich_text": [
            {
              "type": "text",
              "text": {
                "content": "nospam2@gmail.com",
                "link": null
              }
            }
          ]
        }
      }
    }
  ]
}

错误原因

  1. 根节点无properties字段:JSON根对象是list类型,properties嵌套在results数组的每个page对象中,原代码直接访问data['properties']必然触发KeyError。
  2. 邮箱路径错误:原代码查找plain_text字段,但实际邮箱内容存储在properties.email_sender.rich_text[0].text.content路径下。

修正后的代码

import json

TARGET_EMAIL = "nospam1@gmail.com"

with open('notion_data.json') as json_file:
    data = json.load(json_file)

# 遍历results中的每个页面
found = False
for page in data['results']:
    # 检查当前页面是否有email_sender属性
    if 'email_sender' in page['properties']:
        email_prop = page['properties']['email_sender']
        # 提取rich_text中的邮箱内容
        if email_prop['rich_text']:
            email_content = email_prop['rich_text'][0]['text']['content']
            if email_content == TARGET_EMAIL:
                found = True
                break

print("IN" if found else "NOT")

代码说明

  • 加载JSON数据后,遍历results数组中的每个页面对象
  • 针对每个页面,先检查是否存在email_sender属性
  • 若存在,读取rich_text数组中第一个元素的text.content获取邮箱地址
  • 对比目标邮箱,找到匹配项后标记并终止遍历,最后输出结果

内容的提问来源于stack exchange,提问作者Stijn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 18:05:44