본문 바로가기

Python_Intermediate/Json

JSON - Site JSON data analysis(URL JSON 분석)

1. Import Module

import requests
import json
from print_df import print_df


2. Sample Code

- Site URL Response 후 내용 가져와서 출력

import requests
import json
from print_df import print_df

url = 'https://api.androidhive.info/contacts'

response = requests.get(url)

if response.status_code != 200:
print("[%d Error] %s" % (response.status_code, response.reason))
quit()

response.encoding = 'UTF-8'

print_df(response.text)

<class 'str'>

{

    "contacts": [

        {

                "id": "c200",

                "name": "Ravi Tamada",

                "email": "ravi@gmail.com",

                "address": "xx-xx-xxxx,x - street, x - country",

                "gender" : "male",

                "phone": {

                    "mobile": "+91 0000000000",

                    "home": "00 000000",

                    "office": "00 000000"

                }

        },

        {

                "id": "c201",

                "name": "Johnny Depp",

                "email": "johnny_depp@gmail.com",

                "address": "xx-xx-xxxx,x - street, x - country",

                "gender" : "male",

                "phone": {

                    "mobile": "+91 0000000000",

                    "home": "00 000000",

                    "office": "00 000000"

                }

        },

        {

                "id": "c202",

                "name": "Leonardo Dicaprio",

                "email": "leonardo_dicaprio@gmail.com",

                "address": "xx-xx-xxxx,x - street, x - country",

                "gender" : "male",

                "phone": {

                    "mobile": "+91 0000000000",

                    "home": "00 000000",

                    "office": "00 000000"

                }

        },

        {

                "id": "c203",

                "name": "John Wayne",

                "email": "john_wayne@gmail.com",

                "address": "xx-xx-xxxx,x - street, x - country",

                "gender" : "male",

                "phone": {

                    "mobile": "+91 0000000000",

                    "home": "00 000000",

                    "office": "00 000000"

                }

        },

        {

                "id": "c204",

                "name": "Angelina Jolie",

                "email": "angelina_jolie@gmail.com",

                "address": "xx-xx-xxxx,x - street, x - country",

                "gender" : "female",

                "phone": {

                    "mobile": "+91 0000000000",

                    "home": "00 000000",

                    "office": "00 000000"

                }

        },

        {

                "id": "c205",

                "name": "Dido",

                "email": "dido@gmail.com",

                "address": "xx-xx-xxxx,x - street, x - country",

                "gender" : "female",

                "phone": {

                    "mobile": "+91 0000000000",

                    "home": "00 000000",

                    "office": "00 000000"

                }

        },

        {

                "id": "c206",

                "name": "Adele",

                "email": "adele@gmail.com",

                "address": "xx-xx-xxxx,x - street, x - country",

                "gender" : "female",

                "phone": {

                    "mobile": "+91 0000000000",

                    "home": "00 000000",

                    "office": "00 000000"

                }

        },

        {

                "id": "c207",

                "name": "Hugh Jackman",

                "email": "hugh_jackman@gmail.com",

                "address": "xx-xx-xxxx,x - street, x - country",

                "gender" : "male",

                "phone": {

                    "mobile": "+91 0000000000",

                    "home": "00 000000",

                    "office": "00 000000"

                }

        },

        {

                "id": "c208",

                "name": "Will Smith",

                "email": "will_smith@gmail.com",

                "address": "xx-xx-xxxx,x - street, x - country",

                "gender" : "male",

                "phone": {

                    "mobile": "+91 0000000000",

                    "home": "00 000000",

                    "office": "00 000000"

                }

        },

        {

                "id": "c209",

                "name": "Clint Eastwood",

                "email": "clint_eastwood@gmail.com",

                "address": "xx-xx-xxxx,x - street, x - country",

                "gender" : "male",

                "phone": {

                    "mobile": "+91 0000000000",

                    "home": "00 000000",

                    "office": "00 000000"

                }

        },

        {

                "id": "c2010",

                "name": "Barack Obama",

                "email": "barack_obama@gmail.com",

                "address": "xx-xx-xxxx,x - street, x - country",

                "gender" : "male",

                "phone": {

                    "mobile": "+91 0000000000",

                    "home": "00 000000",

                    "office": "00 000000"

                }

        },

        {

                "id": "c2011",

                "name": "Kate Winslet",

                "email": "kate_winslet@gmail.com",

                "address": "xx-xx-xxxx,x - street, x - country",

                "gender" : "female",

                "phone": {

                    "mobile": "+91 0000000000",

                    "home": "00 000000",

                    "office": "00 000000"

                }

        },

        {

                "id": "c2012",

                "name": "Eminem",

                "email": "eminem@gmail.com",

                "address": "xx-xx-xxxx,x - street, x - country",

                "gender" : "male",

                "phone": {

                    "mobile": "+91 0000000000",

                    "home": "00 000000",

                    "office": "00 000000"

                }

        }

    ]

}




Process finished with exit code 0


     - 딕셔너리 변환 후 출력

import requests
import json
from print_df import print_df

url = 'https://api.androidhive.info/contacts'

response = requests.get(url)

if response.status_code != 200:
print("[%d Error] %s" % (response.status_code, response.reason))
quit()

response.encoding = 'UTF-8'

result = json.loads(response.text)
print_df(result)

<class 'dict'>

{'contacts': [{'id': 'c200', 'name': 'Ravi Tamada', 'email': 'ravi@gmail.com', 'address': 'xx-xx-xxxx,x - street, x - country', 'gender': 'male', 'phone': {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'}}, {'id': 'c201', 'name': 'Johnny Depp', 'email': 'johnny_depp@gmail.com', 'address': 'xx-xx-xxxx,x - street, x - country', 'gender': 'male', 'phone': {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'}}, {'id': 'c202', 'name': 'Leonardo Dicaprio', 'email': 'leonardo_dicaprio@gmail.com', 'address': 'xx-xx-xxxx,x - street, x - country', 'gender': 'male', 'phone': {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'}}, {'id': 'c203', 'name': 'John Wayne', 'email': 'john_wayne@gmail.com', 'address': 'xx-xx-xxxx,x - street, x - country', 'gender': 'male', 'phone': {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'}}, {'id': 'c204', 'name': 'Angelina Jolie', 'email': 'angelina_jolie@gmail.com', 'address': 'xx-xx-xxxx,x - street, x - country', 'gender': 'female', 'phone': {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'}}, {'id': 'c205', 'name': 'Dido', 'email': 'dido@gmail.com', 'address': 'xx-xx-xxxx,x - street, x - country', 'gender': 'female', 'phone': {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'}}, {'id': 'c206', 'name': 'Adele', 'email': 'adele@gmail.com', 'address': 'xx-xx-xxxx,x - street, x - country', 'gender': 'female', 'phone': {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'}}, {'id': 'c207', 'name': 'Hugh Jackman', 'email': 'hugh_jackman@gmail.com', 'address': 'xx-xx-xxxx,x - street, x - country', 'gender': 'male', 'phone': {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'}}, {'id': 'c208', 'name': 'Will Smith', 'email': 'will_smith@gmail.com', 'address': 'xx-xx-xxxx,x - street, x - country', 'gender': 'male', 'phone': {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'}}, {'id': 'c209', 'name': 'Clint Eastwood', 'email': 'clint_eastwood@gmail.com', 'address': 'xx-xx-xxxx,x - street, x - country', 'gender': 'male', 'phone': {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'}}, {'id': 'c2010', 'name': 'Barack Obama', 'email': 'barack_obama@gmail.com', 'address': 'xx-xx-xxxx,x - street, x - country', 'gender': 'male', 'phone': {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'}}, {'id': 'c2011', 'name': 'Kate Winslet', 'email': 'kate_winslet@gmail.com', 'address': 'xx-xx-xxxx,x - street, x - country', 'gender': 'female', 'phone': {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'}}, {'id': 'c2012', 'name': 'Eminem', 'email': 'eminem@gmail.com', 'address': 'xx-xx-xxxx,x - street, x - country', 'gender': 'male', 'phone': {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'}}]}




Process finished with exit code 0


- id / gender / phone - mobile Data 출력

import requests
import json
from print_df import print_df

url = 'https://api.androidhive.info/contacts'

response = requests.get(url)

if response.status_code != 200:
print("[%d Error] %s" % (response.status_code, response.reason))
quit()

response.encoding = 'UTF-8'

result = json.loads(response.text)

id = []
gender = []
mobile = []

for i in result['contacts']:
id.append(i['id'])
gender.append(i['gender'])
mobile.append(i['phone']['mobile'])

print_df(id)
print_df(gender)
print_df(mobile)

<class 'list'>

['c200', 'c201', 'c202', 'c203', 'c204', 'c205', 'c206', 'c207', 'c208', 'c209', 'c2010', 'c2011', 'c2012']



<class 'list'>

['male', 'male', 'male', 'male', 'female', 'female', 'female', 'male', 'male', 'male', 'male', 'female', 'male']



<class 'list'>

['+91 0000000000', '+91 0000000000', '+91 0000000000', '+91 0000000000', '+91 0000000000', '+91 0000000000', '+91 0000000000', '+91 0000000000', '+91 0000000000', '+91 0000000000', '+91 0000000000', '+91 0000000000', '+91 0000000000']




Process finished with exit code 0


- Data 가공 출력 1

import requests
import json

url = 'https://api.androidhive.info/contacts'

response = requests.get(url)

if response.status_code != 200:
print("[%d Error] %s" % (response.status_code, response.reason))
quit()

response.encoding = 'UTF-8'

result = json.loads(response.text)

id = []
gender = []
mobile = []

for i in result['contacts']:
id.append(i['id'])
gender.append(i['gender'])
mobile.append(i['phone']['mobile'])

for j in range(len(id)):
print('id : {0}'.format(id[j]))
print('Gender : {0}'.format(gender[j]))
print('phone mobile : {0}'.format(mobile[j]))
print('-' * 20)

id : c200

Gender : male

phone mobile : +91 0000000000

--------------------

id : c201

Gender : male

phone mobile : +91 0000000000

--------------------

id : c202

Gender : male

phone mobile : +91 0000000000

--------------------

id : c203

Gender : male

phone mobile : +91 0000000000

--------------------

id : c204

Gender : female

phone mobile : +91 0000000000

--------------------

id : c205

Gender : female

phone mobile : +91 0000000000

--------------------

id : c206

Gender : female

phone mobile : +91 0000000000

--------------------

id : c207

Gender : male

phone mobile : +91 0000000000

--------------------

id : c208

Gender : male

phone mobile : +91 0000000000

--------------------

id : c209

Gender : male

phone mobile : +91 0000000000

--------------------

id : c2010

Gender : male

phone mobile : +91 0000000000

--------------------

id : c2011

Gender : female

phone mobile : +91 0000000000

--------------------

id : c2012

Gender : male

phone mobile : +91 0000000000

--------------------


Process finished with exit code 0


- Data 가공 출력 2(Gender = male 회원만 출력)

import requests
import json

url = 'https://api.androidhive.info/contacts'

response = requests.get(url)

if response.status_code != 200:
print("[%d Error] %s" % (response.status_code, response.reason))
quit()

response.encoding = 'UTF-8'

result = json.loads(response.text)

id = []
gender = []
mobile = []

for i in result['contacts']:
id.append(i['id'])
gender.append(i['gender'])
mobile.append(i['phone']['mobile'])

for j in range(len(id)):
if gender[j] == 'male':
print('id : {0}'.format(id[j]))
print('Gender : {0}'.format(gender[j]))
print('phone mobile : {0}'.format(mobile[j]))
print('-' * 20)
else:
pass

id : c200

Gender : male

phone mobile : +91 0000000000

--------------------

id : c201

Gender : male

phone mobile : +91 0000000000

--------------------

id : c202

Gender : male

phone mobile : +91 0000000000

--------------------

id : c203

Gender : male

phone mobile : +91 0000000000

--------------------

id : c207

Gender : male

phone mobile : +91 0000000000

--------------------

id : c208

Gender : male

phone mobile : +91 0000000000

--------------------

id : c209

Gender : male

phone mobile : +91 0000000000

--------------------

id : c2010

Gender : male

phone mobile : +91 0000000000

--------------------

id : c2012

Gender : male

phone mobile : +91 0000000000

--------------------


Process finished with exit code 0