Home > Net >  How to filter json information into multiple values?
How to filter json information into multiple values?

Time:11-17

I'm looking for a way to filter client information into variables that i can use to send emails. One of the variables im looking for is "[email protected]" Can somebody help me with this?

The code i tested is:

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')=='[email protected]']:
  print("IN")
else:
  print("NOT")

The error i get: 
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')=='[email protected]']:
                   ~~~~^^^^^^^^^^^^^^
KeyError: 'properties'

Process finished with exit code 1

Data of the json file:

{
  "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": "[email protected]",
                "link": null
              }}}}}

CodePudding user response:

You have to dive through ALL of the intermediate objects. Assuming there are multiple results:

for result in data['results']:
    texttype = result['properties']['email_sender']['type']
    email = result['properties']['email_sender'][texttype][0]['text']['content']
    if email == '[email protected]':
        print("winner")
  • Related