0

I have to get data from a json file but the values that I need are the child of a greater value. (it's a bit hard to explain). Basically the file sooks like this:

{
    "records": [
        {
            "id": "reck8UMt9Kd2C5o05",
            "fields": {
                "start_date": "2021-06-02T22:00:00.000Z",
                "ei_ready": true,
                "⚙️rw_ecpm": 12.5,
                "adset_id": "60bf50a4321707001074c113",
                "⚙️dyn_standard": 65,
                "production_status": "Prozessiert",
                "copy2": "Vom Samstag, 10. April, ab 18 Uhr, bis Donnerstag, 22. April 2021",
                "copy1": "Fahrplanänderungen Brugg AG-Turgi-Baden."
            },
            "createdTime": "2021-06-04T07:17:08.000Z"
        }
    ]
}

How can I get only the copy2 or copy1 value from this json file? I tried something like this but it does not work. It's like I have to look for a file in a folder that is in a folder:

copy1 = (ATdata["records"],{},{"fields"},["copy1"])
print("copy1")# and only copy1 not anything else

Thank you

  • Your JSON and your suggested code have nothing to do with each other.. What is `landing_page` in the JSON, and for that matter what does any of this have to do with subfolders? – Silvio Mayolo Jun 09 '21 at 17:51
  • its just another value like copy1 or copy2 il edit the question – Gabriel Ferguson Jun 09 '21 at 17:52
  • You want copy1 and copy2, yet you try to read the landing_page field? There is no landing_page field in your json. Show you code of how you load the json, and how you reference the elements. – Nic3500 Jun 09 '21 at 17:53
  • this is the code ignore the landing page thing it was a mistake. All I want to know is how to get the copy1 form this json – Gabriel Ferguson Jun 09 '21 at 18:02
  • No, not really. I need to be able to just filter out copy1 from that json – Gabriel Ferguson Jun 09 '21 at 18:12

1 Answers1

0

records key of your json is the root key which contains list of dictionaries, therefore records [0] is your desired document.

Now your desired document again contain one more dictionaries in fields and your desired key that is copy1 or copy2 is inside it, therefore you can access it like that :

j = {your json given above}
j["records"] [0]["fields"]["copy1"]
j["records"] [0]["fields"]["copy2"]

Now if you are having more than one document inside that records list. You have to do it like this:

Copy1=[] #to store all copy1 here
Copy2=[]
for i in j["records"]:
    Copy1.append(i["fields"]["copy1"])
    Copy2.append(i["fields"]["copy2"]) #here i is each document