pythonjsonjsonschemalinked-data

JSON schema nesting based on linked data


I've dataset pulled from a linked data platform. The dataset looks like this:

label relationClass
Organization Department
Department Employee

I want to create a JSON Schema based on this data where the hierarchy between objects is nested.

The decomposition of the hierarchy look something like this:

Organization

Eventually the parsing should result in a JSON Schema looking like this:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "organization": {
      "type": "object",
      "properties": {
        "department": {
          "type": "object",
          "properties": {
            "employee": {
              "type": "object"
            }
          }
        }
      }
    }
  }
}

Can someone help out with the most efficient way to achieve this?


Solution

  • It looks like a classical tree structure. For optimal performance you'd go over it once and build a tree/directed graph from it, then recursively traverse in preorder to create all the children of the nodes as object.

    Searching for 'build tree from list of pairs' yielded the following SO question with a working answer: Given a flat list of (parent,child), create a hierarchical dictionary tree