Is it possible to use yjs for collaboration over a nested object (modeled as a JSON object)?)?


My document model doesn’t fit a simple Map/List/Text structure. Does yjs support CRDTs over a JSON model?


Hi @nmichaud,

The Yjs data types are more expressive as JSON. You can model any JSON document using Yjs types. I.e.

  key: [1, 2]

Can be modeled like this:

const ymap = ydoc.getMap('my-json')
const yarray = new Y.Array()
yarray.insert(0, [1, 2])
ymap.set('key', yarray)
ymap.toJSON() // => { key: [1, 2] }

JSON is just an encoding technique. In JavaScript, you can efficiently express complex data structures using the JavaScript Object Notation consisting of Object, Array, and primitive data types (number, string, …). You can model any Object as a Y.Map, and you can model any Array as a Y.Array. The above Yjs example is a bit more complex because you can’t make use of the JavaScript Object Notation. That is the only difference. Hopefully, we can eventually make expressing complex structures more efficient.

Ah that’s great! I didn’t realize the Y types could be nested within each other (ie a Y.Array or Y.Map within a top level Y.Map). That’s exactly what i was looking for.

Great! :slight_smile: