Store JSON as Graphs

Store and query JSON documents as graphs in CogDB.

As of version 3.0.2 JSONs can be inserted into a graph.

putj

putj(json)

Inserts a JSON object into a graph. Each object is identified by a BlankNode with a unique label.

Example JSON:

{"name": "bob", "location": {"city": "toronto", "country": "canada"}}

Becomes these triples:

_:abc  →  name      →  bob
_:abc  →  location  →  _:def
_:def  →  city      →  toronto
_:def  →  country   →  canada

updatej

updatej(json)

Updates a JSON object. Must include _id property for the root object.


Example

g = Graph("followers")
g.putj('{"name": "bob", "status": "cool_person", "follows": ["fred", "dani"]}')
g.putj('{"_id": "1", "name": "fred", "status": "cool_person", "follows": ["alice", "greg"]}')
g.putj('{"name": "alice", "status": "cool_person", "follows": ["bob", "dani"]}')

Queries

g.v().out("name").all()
# {'result': [{'id': 'bob'}, {'id': 'alice'}, {'id': 'fred'}]}

g.v().has("name", "bob").out("status").all()
# {'result': [{'id': 'cool_person'}]}

g.v().has("name", "alice").out("follows").out().all()
# {'result': [{'id': 'dani'}, {'id': 'bob'}]}

Updating JSON

g.updatej('{"_id": "1", "status": "not_cool"}')

g.v().has("name", "fred").out("status").all()
# {'result': [{'id': 'not_cool'}]}

On this page