JSON-LD updates via Stardog HTTP API

Not sure if that exists yet:

  • Can I use some HTTP API to add/import JSON-LD data (a python dictionary) to my knowledge graph?

I know one can do stardog data addhttps://www.stardog.com/docs/man/data-add. But that would require creating an intermediary file.

i know you haven't forgotten about this question ... but just in case :wink:

Sorry about the delay! You can indeed add data via HTTP, however it requires 3 calls:

  1. POST /{db}/transaction/begin returns a transaction ID
  2. POST /{db}/{txid}/add with Content-Type: application/ld+json in the header to add the JSON-LD in the context of the transaction you began
  3. POST /{db}/transaction/commit/{txid} to commit the transaction

Radu,

Alternatively you can POST using the SPARQL graph store protocol to load the JSON-LD data into a specific graph using only one HTTP call

Stephen,

I will try the three steps above - but it is a hack...

  • Don't you plan to support it like you plan to do so for stardog-admin import ... json over HTTP API at the end of this month?

Regards,
Radu

It's definitely not a hack, it is designed this way. When you do a stardog data add via the CLI, behind the scenes it is executing these 3 steps. Databases executing write operations within transactions is the standard. It allows you to rollback without affecting the end result if a mistake is made.

Stephen,

I see your point about "rollback without affecting the end result if a mistake is made" - makes sense.

I have a question regarding " POST using the SPARQL graph store protocol to load the JSON-LD data into a specific graph using only one HTTP call":

  • So instead of "Content-Type: text/turtle" I would use "Content-Type: application/ld+json", right?

The post url on my side looks like "hostname:port/DBName/update"

Regards,
Radu

Hi Radu,

This is exactly right. The URL that you posted, however, is the endpoint that accepts SPARQL update queries. To use the graph store protocol, you would need to POST to hostname:port/DBName?graph=graph_uri

Stephen,

And if I don't specify "graph=graph_uri" - what would happen?

Let's say I want to achieve the same results as if I would do stardog data add db-name file.jsonld - would that be loaded into some kind of default graph? What is the "graph_uri" for default graph?

Hope you don't mind my questions - I have never done it before...

Thanks,
Radu

For the graph store protocol (according to the spec), you always need to provide a graph uri, though for the default graph you merely have to POST to hostname:port/DBName?default.

And yes, that would behave exactly like a stardog data add without specifying a graph URI

1 Like

In this specific case, since you're using an RDF file format that supports graphs, you can ignore the graph/default query string stuff altogether and just POST hostname:port/DBName. The graphs in the RDF file will be respected.