# JSON-LD in SOLID-CLIENT

**URL:** <https://forum.solidproject.org/t/json-ld-in-solid-client/5722>\
**Category:** Solid App Development FAQs\
**Created:** [October 12, 2022, 1:20am UTC](https://forum.solidproject.org/t/json-ld-in-solid-client/5722 "2022-10-12T01:20:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielbakas](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/danielbakas/32/2766_2.png) [@danielbakas](https://forum.solidproject.org/u/danielbakas)\
**Post date:** [October 12, 2022, 1:20am UTC](https://forum.solidproject.org/t/json-ld-in-solid-client/5722/1 "2022-10-12T01:20:18Z")

</div>

Hi everyone!! Quick and simple question: **How can we parse/serialize a Thing to JSON-LD using the `solid-client` framework?**

This is what I got so far:

```auto
  // Get Dataset
  const endpoint = "https://path.to/pod/container/";
  const options = { fetch: session.fetch };
  const dataset = await getSolidDataset(endpoint, options);
  // Get Thing
  const thing = getThing(pod, endpoint);
  // THIS IS WHERE I'M STUCK
  const result = {code that magically turns thing into JSON-LD}
  // Return JSON-LD formatted Thing
  res.send(result);

```

Thank you so much! And if this feature is not yet available I would love to know where I can post a ticket or issue to suggest it 😋 Cheers!

---

<div class="post-metadata">

**Author:** ![ThisIsMissEm](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/thisismissem/32/3166_2.png) [@ThisIsMissEm](https://forum.solidproject.org/u/ThisIsMissEm)\
**Post date:** [October 13, 2022, 12:10am UTC](https://forum.solidproject.org/t/json-ld-in-solid-client/5722/2 "2022-10-13T00:10:24Z")

</div>

At present we don’t have direct functionality in `@inrupt/solid-client` for this. Off top of head, you’d need to go through `toRdfJsDataset` and then use that to serialise to JSON-LD.

As an aside, in your code above the first argument to `getThing` would be `dataset` not `pod`.

Docs: [Module: rdfjs — Inrupt solid-client Documentation](https://docs.inrupt.com/developer-tools/api/javascript/solid-client/modules/rdfjs.html#tordfjsdataset)

You may also like: [GitHub - digitalbazaar/jsonld.js: A JSON-LD Processor and API implementation in JavaScript](https://github.com/digitalbazaar/jsonld.js)

---

<div class="post-metadata">

**Author:** ![danielbakas](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/danielbakas/32/2766_2.png) [@danielbakas](https://forum.solidproject.org/u/danielbakas)\
**Post date:** [October 13, 2022, 7:33am UTC](https://forum.solidproject.org/t/json-ld-in-solid-client/5722/3 "2022-10-13T07:33:32Z")

</div>

Here’s how the `solidThing.toJSONLD()` method for converting a single thing from a Solid Dataset, to JSON-LD looks to me at the moment:

```javascript
// 1. **`getDataset`** : Retrieves all things from a container in a given endpoint.
const pod = await getSolidDataset(endpoint, options);
// 2. **`getThing`** : Retrieves a single thing from the Solid dataset
const thing = getThing(pod, endpoint);
// 3. **`createSolidDataset`** : Create a new Solid dataset just for that thing
let dataset = createSolidDataset();
// 4. **`setThing`** : Set that thing to its dedicated Solid dataset
dataset = setThing(dataset, thing);
// 5. **`toRdfJsDataset`** : Serialize that Solid dataset to an RDF/JS dataset
const rdfJsDataset = toRdfJsDataset(dataset);
// 6. **`rdfJsDataset.match`** : Serialize the RDF/JS dataset to Quads
const quads = rdfJsDataset.match();
// 7. **`new JsonLdSerializer()`**: Create a new JSON-LD serializer
const serialiser = new JsonLdSerializer();
// 8. **`serializer.write(quad)`**: Add every quad to the serializer
for (const quad of quads)
  serialiser.write(quad);
serialiser.end();
// 9. **`stringifyStream(serializer)`**: Generate string with JSON-LD resulting serialization
const result = stringifyStream(serialiser);
// 10. **`JSON.parse`** : Parse JSON-LD string to JSON for use in code
const parsed = JSON.parse(await result);

```

Additionally, one could compact that JSON-LD with a given context using:

```javascript
const compact = await jsonld.compact(parsed, context);

```

Now, the question becomes: **Is this optimal though? Is there a better way to do this?** 😯

Thank you so very much @ThisIsMissEm

---

<div class="post-metadata">

**Author:** ![jeswr](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/jeswr/32/3924_2.png) [@jeswr](https://forum.solidproject.org/u/jeswr)\
**Post date:** [October 13, 2022, 11:56am UTC](https://forum.solidproject.org/t/json-ld-in-solid-client/5722/4 "2022-10-13T11:56:04Z")

</div>

Forwarding [Solid to JSON-LD serialization? · Issue #1732 · inrupt/solid-client-js · GitHub](https://github.com/inrupt/solid-client-js/issues/1732#issuecomment-1277231260) for completeness

You could always take a query approach like this depending on your use case and skip the conversion to and from thing.

Here the source is just your endpoint

```auto

import { QueryEngine } from '@comunica/query-sparql-solid';
import { type Session } from '@inrupt/solid-client-authn-browser';
const stringifyStream = require('stream-to-string');

async function getJsonLD(webId: string, source: string, session: Session) {
  const engine = new QueryEngine();

  const result = await engine.resultToString(
    await engine.query(`CONSTRUCT { <${webId}> ?p ?o } WHERE { <${webId}> ?p ?o }`, {
      sources: [source],
      '@comunica/actor-http-inrupt-solid-client-authn:session': session
    }),
    'application/ld+json'
  );

  return stringifyStream(result.data)
}

```

---

<div class="post-metadata">

**Author:** ![Vincent](https://avatars.discourse-cdn.com/v4/letter/v/e9bcb4/32.png) [@Vincent](https://forum.solidproject.org/u/Vincent)\
**Post date:** [October 13, 2022, 2:18pm UTC](https://forum.solidproject.org/t/json-ld-in-solid-client/5722/5 "2022-10-13T14:18:54Z")

</div>

Ah, I just realised that you can also skip solid-client for getting the Thing, since you’re already calling `match`, which does that:

```nohighlight
// 1. **`getDataset`** : Retrieves all things from a container in a given endpoint.
const pod = await getSolidDataset(endpoint, options);
// 2. **`toRdfJsDataset`** : Serialize that Solid dataset to an RDF/JS dataset
const rdfJsDataset = toRdfJsDataset(pod);
// 3. **`rdfJsDataset.match`** : Extract the RDF/JS dataset's Quads that have `endpoint` as their Subject (i.e. a Thing)
const quads = rdfJsDataset.match(endpoint);
// 4. **`new JsonLdSerializer()`**: Create a new JSON-LD serializer
const serialiser = new JsonLdSerializer();
// 5. **`serializer.write(quad)`**: Add every quad to the serializer
for (const quad of quads)
  serialiser.write(quad);
serialiser.end();
// 6. **`stringifyStream(serializer)`**: Generate string with JSON-LD resulting serialization
const result = stringifyStream(serialiser);
// 7. **`JSON.parse`** : Parse JSON-LD string to JSON for use in code
const parsed = JSON.parse(await result);

```

---

<div class="post-metadata">

**Author:** ![danielbakas](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/danielbakas/32/2766_2.png) [@danielbakas](https://forum.solidproject.org/u/danielbakas)\
**Post date:** [October 13, 2022, 9:09pm UTC](https://forum.solidproject.org/t/json-ld-in-solid-client/5722/6 "2022-10-13T21:09:58Z")

</div>

That also works! Thank you @Vincent! Although performance wise, Comunica rocks! 🤘🏽

---

<div class="post-metadata">

**Author:** ![wahed](https://avatars.discourse-cdn.com/v4/letter/w/57b2e6/32.png) [@wahed](https://forum.solidproject.org/u/wahed)\
**Post date:** [November 18, 2022, 8:21pm UTC](https://forum.solidproject.org/t/json-ld-in-solid-client/5722/7 "2022-11-18T20:21:05Z")

</div>

Hi,

Is there any format available for storing VC(Verifiable Credential) into solid pod?

If possible, then is it JSON-LD format or any other type? What is it?

---

<div class="post-metadata">

**Author:** ![ThisIsMissEm](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/thisismissem/32/3166_2.png) [@ThisIsMissEm](https://forum.solidproject.org/u/ThisIsMissEm)\
**Post date:** [November 18, 2022, 9:58pm UTC](https://forum.solidproject.org/t/json-ld-in-solid-client/5722/8 "2022-11-18T21:58:55Z")

</div>

I believe you asked this here: [Storing user preferences in a POD - #8 by nicolasmondada](https://forum.solidproject.org/t/storing-user-preferences-in-a-pod/2194/8)

Please don’t ask the same question in multiple places, it makes it hard for folks to find answers in the future.

---

<div class="post-metadata">

**Author:** ![wahed](https://avatars.discourse-cdn.com/v4/letter/w/57b2e6/32.png) [@wahed](https://forum.solidproject.org/u/wahed)\
**Post date:** [November 19, 2022, 6:25am UTC](https://forum.solidproject.org/t/json-ld-in-solid-client/5722/9 "2022-11-19T06:25:40Z")

</div>

Sorry, my bad. Thank you for informing me.
