Created
March 10, 2009 04:36
-
-
Save edsu/76729 to your computer and use it in GitHub Desktop.
convert an rdflib graph to json using talis flavored json
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 simplejson | |
import rdflib | |
def json_for_graph(g): | |
""" | |
Pass in a rdflib.Graph and get back a chunk of JSON using | |
the Talis JSON serialization for RDF: | |
http://n2.talis.com/wiki/RDF_JSON_Specification | |
""" | |
json = {} | |
# go through all the triples in the graph | |
for s, p, o in g: | |
# initialize property dictionary if we've got a new subject | |
if not json.has_key(s): | |
json[s] = {} | |
# initialize object list if we've got a new subject-property combo | |
if not json[s].has_key(p): | |
json[s][p] = [] | |
# determine the value dictionary for the object | |
v = {'value': unicode(o)} | |
if isinstance(o, rdflib.URIRef): | |
v['type'] = 'uri' | |
elif isinstance(o, rdflib.BNode): | |
v['type'] = 'bnode' | |
elif isinstance(o, rdflib.Literal): | |
v['type'] = 'literal' | |
if o.language: | |
v['lang'] = o.language | |
if o.datatype: | |
v['datatype'] = unicode(o.datatype) | |
# add the triple | |
json[s][p].append(v) | |
return simplejson.dumps(json, indent=2) | |
if __name__ == '__main__': | |
g = rdflib.ConjunctiveGraph() | |
g.load('http://inkdroid.org/ehs') | |
print json_for_graph(g) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment