Complex nested json data to dgraph

My friend, please use code block formatting! (three backticks before and after the code)

To answer your question, there is not a one size fits all for storing JSON data in Dgraph. You title, calls your data “complex nested”, but the data schema is not really all that complex in terms of JSON. You should be able to write this data directly and query it back out with DQL. What you will need to watch out for and be aware of is the following:

  1. Handling of “Arrays” in Dgraph. Dgraph treats arrays like sets, so duplicates and order are not preserved.
  2. Lack of support for multi-dimensional arrays. I don’t see this in your dataset example, but just be aware of it.
  3. Predicate naming/typing. I don’t see this problem in your dataset either, but for instance, if in one place you used the key (aka predicate) “foo” to map to a string, and then later “foo” maps to an array or object, or even DateTime then you will have problems. The workaround to this is to prepend the type to the predicate like Dgraph does under the hood with GraphQL schema.
  4. Deep updating without having uids. This might not be that evident if just saving JSON data and not needing to modify it, but when you do need to modify it, modifying a nested portion of it can be tricky to do and usually you will find yourself appending to existing adding new nested nodes instead of modifying the existing nested nodes.
  5. Typing nested data. If you want your data to work with Dgraph types, then you will need to manually add the dgraph.type predicates to every object in your JSON data.

A workaround to all of this is to store the JSON as a string, but that might really take away the value of using Dgraph for your use case.