Question

I'm trying to get my head around the dbpedia JSON schema and can't figure out an efficient way of extracting a specific node:

This is what dbpedia gives me:

http://dbpedia.org/data/Ceramic_art.json

I've got the whole thing as a JSON object in Python but don't really understand how to get the english abstract from this data. I've gotten this far:

u = "http://dbpedia.org/data/Ceramic_art.json"
data = urlfetch.fetch(url=u)
json_data = json.loads(data.content)

for j in json_data["http://dbpedia.org/resource/Ceramic_art"]:
    if(j == "http://dbpedia.org/ontology/abstract"):
        print "it's here"

Not sure how to proceed from here. As you can see there are multiple languages. I need to get the english abstract.

Thanks for your help,

g

Was it helpful?

Solution


print [abstract['value'] for abstract in json_data["http://dbpedia.org/resource/Ceramic_art"]["http://dbpedia.org/ontology/abstract"] if abstract['lang'] == 'en'][0]

Obviously, you'd want to do more error checking than that, in case the data is bad, but that's the basic idea.

OTHER TIPS

It's a list of dicts. Just iterate through the elements of the list until you find the one whose value for u'lang' is u'en'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top