Extracting Data from JSON

Hello guys, I’m working on my own little project for some practice because I’m struggling with the FCC ones.

I’m trying to make an app that will guess your nationality, age and gender, based on your name. The actual guessing work is all done by API’s which already exist, I am just trying to combine the output data from all three based on user input.

I am struggling to extract a certain piece of the JSON that i need for the next part of my code to work. This is what I have so far:

# Profiler guesses your age, gender and nationality
import requests
import json

# User's input
my_nam = input("Please input your first name here: ")

# Nationality
response = requests.get('https://api.nationalize.io/?name='+my_nam)
data = response.json()
data = (data['country'][0])

This text will be hidden

The output I get is:

Please input your first name here: laurence
{'country_id': 'RE', 'probability': 0.1806635914207585}

With “laurence” being the user input.
I would like to take the ‘RE’ from this response so that I can use it to append the input for the next API which takes your name and nationality and then guesses your age. I just need the ‘RE’ part of this to add to the end of the URL .

for example:

Anyone have any ideas on how I can get this?

Consider the way contents of the country key is obtained from data. The same approach can be taken later, to get country_id value, when data = (data['country'][0]).

Hey sanity,

Thanks once again for the feedback. Manged to get it all working now. Simply input your name and it guesses what nationality, age and gender you are. It’s pretty need and quite accurate with some names!.
Checking out PyQt at the moment to implement some form of GUI.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.