solid

Solid: correct way to store a JSON object


I want to store a standard JSON object in the user’s Solid pod. After working through the Solid getting started tutorial, I found I could get/set the object in the VCARD.note parameter, but I suspect this is not the right way to do it.

Any advice on how to do this properly? The JSON object will be updated regularly and will typically have ~10-100 key pairs.


Solution

  • There are two options here.

    Option 1 - store as RDF (recommended)

    Generally, the recommended thing to do is not to store data as a standard JSON object, but rather to save the data as RDF. So for example, if you have a JSON object like

    const user = {
      name: "Vincent",
    };
    

    assuming you're using the JavaScript library @inrupt/solid-client, you'd create what it calls a "Thing" like this:

    import { createThing, addStringNoLocale } from "@inrupt/solid-client";
    import { foaf } from "rdf-namespaces";
    
    let userThing = createThing();
    userThing = addStringNoLocale(userThing, foaf.fn, "Vincent");
    

    You can read more about this approach at https://docs.inrupt.com/developer-tools/javascript/client-libraries/tutorial/read-write-data/

    Option 2 - store a JSON blob directly

    The other option is indeed to store a JSON file directly in the Pod. This works, although it goes a bit against the spirit of Solid, and requires you to overwrite the complete file every time, rather than allowing you to just update individual properties whenever you update the data. You could do that as follows:

    import { overwriteFile } from "@inrupt/solid-client";
    
    const user = {
      name: "Vincent",
    };
    
    // This is assuming you're working in the browser;
    // in Node, you'll have to create a Buffer instead of a Blob.
    overwriteFile(
      "https://my.pod/location-of-the-file.json",
      new Blob([
        JSON.stringify(user),
      ]),
      { type: "application/json" },
    ).then(() => console.log("Saved the JSON file.}));
    

    You can read more about this approach here: https://docs.inrupt.com/developer-tools/javascript/client-libraries/tutorial/read-write-files/