Skip to content

Instantly share code, notes, and snippets.

Created March 10, 2009 04:36
Show Gist options
  • Save edsu/76729 to your computer and use it in GitHub Desktop.
Save edsu/76729 to your computer and use it in GitHub Desktop.
convert an rdflib graph to json using talis flavored json
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:
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
return simplejson.dumps(json, indent=2)
if __name__ == '__main__':
g = rdflib.ConjunctiveGraph()
print json_for_graph(g)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment