Store JSON as Graphs
Store and query JSON documents as graphs in CogDB.
CogDB allows you to store and query JSON documents directly within your graph architecture. Let's look at how to insert and update JSON objects.
putj
putj(json)This method inserts a JSON object into your graph. CogDB identifies each object using a BlankNode with a unique label.
For example, if you insert the following JSON:
{"name": "bob", "location": {"city": "toronto", "country": "canada"}}It is automatically converted into these triples:
_:abc → name → bob
_:abc → location → _:def
_:def → city → toronto
_:def → country → canadaupdatej
updatej(json)Use this method to update an existing JSON object in the graph. You must include the _id property in the root object so CogDB knows which specific node to update.
Example
Here is how you can use these methods to populate a graph:
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
Once your JSON is stored, you can traverse it just like any other graph data:
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
If you need to change a property later, pass the _id along with the new values:
g.updatej('{"_id": "1", "status": "not_cool"}')
g.v().has("name", "fred").out("status").all()
# {'result': [{'id': 'not_cool'}]}