2

I apologize for the fairly basic question, but I seem to be struggling to iterate through anything other than the keys on this JSON array/dictionary.

import requests

villager_url = requests.get("https://acnhapi.com/v1/villagers")
villagers = villager_url.json()

for villager in villagers:
    print(villager)

the request is set up pretty basic and right now I can get a list of the keys, but I am struggling to print out anything that is contained within the array that makes up the value of the dictionary.

{
"ant00": {
"id": 1,
"file-name": "ant00",
"name": {
"name-USen": "Cyrano",
"name-EUen": "Cyrano",
"name-EUde": "Theo",
"name-EUes": "Cirano",
"name-USes": "Cirano",
"name-EUfr": "Cyrano",
"name-USfr": "Cyrano",
"name-EUit": "Cirano",
"name-EUnl": "Cyrano",
"name-CNzh": "阳明",
"name-TWzh": "陽明",
"name-JPja": "さくらじま",
"name-KRko": "사지마",
"name-EUru": "Сирано"
},
"personality": "Cranky",
"birthday-string": "March 9th",
"birthday": "9/3",
"species": "Anteater",
"gender": "Male",
"subtype": "B",
"hobby": "Education",
"catch-phrase": "ah-CHOO",
"icon_uri": "https://acnhapi.com/v1/icons/villagers/1",
"image_uri": "https://acnhapi.com/v1/images/villagers/1",
"bubble-color": "#194c89",
"text-color": "#fffad4",
"saying": "Don't punch your nose to spite your face.",
"catch-translations": {
"catch-USen": "ah-CHOO",
"catch-EUen": "ah-CHOO",
"catch-EUde": "schneuf",
"catch-EUes": "achús",
"catch-USes": "achús",
"catch-EUfr": "ATCHOUM",
"catch-USfr": "ATCHOUM",
"catch-EUit": "ett-CCIÙ",
"catch-EUnl": "ha-TSJOE",
"catch-CNzh": "有的",
"catch-TWzh": "有的",
"catch-JPja": "でごわす",
"catch-KRko": "임돠",
"catch-EUru": "апчхи"
}
}
}

above is the json of the first two key-value pairs. I'm just trying to be able to return a value buried in the value part of the dictionary. I'm mostly looking to return things like the name-USen or possibly a pair of data from the values with the ultimate goal to have a basic web app that will like you put in your birthday and get paired with a villager or something similar. I'm sure this is a no brainer for a lot of you so i apologize. Thank you in advance.

1 Answer 1

1

Maybe it is to help you:

import requests

villager_url = requests.get("https://acnhapi.com/v1/villagers")
villagers = villager_url.json()

dct = {villagers[k]["name"]["name-EUen"]:villagers[k]["birthday-string"] for k in villagers.keys()}

print(dct)
Sign up to request clarification or add additional context in comments.

2 Comments

This seems to work, thank you. if I wanted to look up this process to see how it is working what should I check? I thought it looked like dict comprehension, but that doesn't seem right.
@cerealfordinner Yes, it is dict comprehension.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.