Last active
November 1, 2018 17:04
-
-
Save nickolasteixeira/1675a7f15cb38a52b30a67e6d223db33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
def count_keys(data): | |
total = 0 | |
for i in data: | |
total += 1 | |
if type(data[i]) is dict: | |
total += count_keys(data[i]) | |
elif type(data[i]) is list: | |
for j in data[i]: | |
total+= count_keys(j) | |
return total | |
def longest_key_value(data): | |
length = 0 | |
key = "" | |
for i in data: | |
if len(data[i]) > length: | |
length = len(data[i]) | |
key = i | |
if type(data[i]) is dict: | |
temp, n_len = longest_key_value(data[i]) | |
if n_len > length: | |
key = temp | |
length = n_len | |
elif type(data[i]) is list: | |
for j in data[i]: | |
temp, n_len = longest_key_value(j) | |
if n_len > length: | |
key = temp | |
length = n_len | |
return key, length | |
r = requests.get(feed_url) | |
if r.status_code == 200: | |
data = r.json() | |
total = count_keys(data) | |
print(total) | |
key, length = longest_key_value(data) | |
print(key) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment