I'm working with opensea response file json, try to get info about nft
json file https://paste.pythondiscord.com/yazaxipiwe full json file https://cdn.discordapp.com/attachments/303906514266226689/1028843885906116658/opensea.json
im getting problems with extracting traits data, traits have daynamic data like some have null traits data and some have 4 to 8 traits data
how to extract like this traits
name link traits_type(eye) traits_type(fur) traits_type(mouth)
APE 0 https://lh3.google.com 3d eye red open-mouth
APE1 https://lh3.google.com brown robot moster
APE2 https://lh3.google.com white green big teeth
APE3 https://lh3.google.com monster eye brown sharp teeth
code
file = json.load(open('opensea.json'))
name.append(file['name'])
link.append(file['image_url'])
but how do get traits data?
It may need to use for
-loop to convert it to dictionary with {type:value}
data = {}
for item in file['traits']:
key = item['trait_type']
value = item['value']
data[key] = value
print(key, value)
and later you can create row (using .get()
you can set default value when item doesn't exist)
row = [
file['name'],
file['image_url'],
data.get('Eye', ''),
data.get('Fur', ''),
data.get('Mouth', '')
]
and add to list
all_rows.append(row)
and later convert all to DataFrame
df = pd.DataFrame(all_rows, columns=['name', 'link', 'traits_type(eye)', 'traits_type(fur)', 'traits_type(mouth)']
import json
import pandas as pd
file = json.load(open('opensea.json'))
print(file.keys())
all_rows = []
for asset in file['assets']:
data = {}
for item in asset['traits']:
key = item['trait_type']
value = item['value']
data[key] = value
#print(key, value)
row = [
asset['name'],
asset['image_url'],
data.get('Fur', ''),
data.get('Eyes', ''),
data.get('Mouth', '')
]
all_rows.append(row)
for item in all_rows:
print(item)
df = pd.DataFrame(all_rows, columns=['name', 'url', 'fur', 'eyes', 'mouth'])
print(df[['name', 'fur', 'eyes', 'mouth']])
Result:
name fur eyes mouth
0 None Robot X Eyes Discomfort
1 None Robot Blue Beams Grin
2 None Robot 3d Bored Cigarette
3 None Cheetah Bored Tongue Out
4 None Golden Brown Closed Phoneme L
5 None Brown X Eyes Dumbfounded
6 None Cream Crazy Bored
7 None Golden Brown Angry Bored
8 None Zombie Robot Bored
9 None Brown Sleepy Small Grin
10 None Dmt Eyepatch Bored
11 None Dark Brown Bloodshot Bored Unshaven Cigarette
# ...