shamilnabiyev / schema-visualizer

Data modeling tool for JSON Documents, JSON-Schema and document based NoSQL Databases
MIT License
22 stars 2 forks source link

Try to flatten the JSON to replace the recursion #11

Open shamilnabiyev opened 4 years ago

shamilnabiyev commented 4 years ago

Use the flattening approach to replace the recursive iteration through the JSON-Docs. The flattened JSON will help to add all child cells of the DiagramRoot at the same time and thus to avoid the z-index problem.

See the example below:

Input:

{
  "id": 1,
  "str": "aaaaa",
  "num": 123,
  "bool": true,
  "obj": {
    "a": true,
    "b": 12.222,
    "c": "texttt",
    "d": { "id": 1 }
  },
  "arr": [
    1,
    2,
    3
  ]
}

Flattened output:

{
    "id": 1,
    "str": "aaaaa",
    "num": 123,
    "bool": true,
    "obj.a": true,
    "obj.b": 12.222,
    "obj.c": "texttt",
    "obj.d.id": 1,
    "arr[0]": 1,
    "arr[1]": 2,
    "arr[2]": 3
}

Links:

http://fiddle.jshell.net/blowsie/S2hsS/show/light/ https://www.npmjs.com/package/flat https://stackoverflow.com/questions/19098797/fastest-way-to-flatten-un-flatten-nested-json-objects

shamilnabiyev commented 4 years ago

If there is a dot in the field name, it can be filtered out:

Input:

{
  "obj": {
    "a": true,
    "d.": {
      "id": 1
    }
  }
}

Flattened output:

{
  "obj.a": true,
  "obj.d..id": 1
}

Extract field name(s):

// split the string
"obj.d..id".split(".")
// returns: [ "obj", "d", "", "id" ]

// split and filter the string
"obj.d..id".split(".").filter(item => item !== "")
// returns: [ "obj", "d", "id" ]
shamilnabiyev commented 4 years ago

schema.properties

{
  "id": {"type": "integer"},
  "str": {"type": "string"},
  "arr": {
    "type": "array",
    "items": [
      {"type": "integer"},
      {"type": "string"},
      {"type": "boolean"},
      {"type": "number"},
      {
        "type": "object",
        "properties": {
          "empty": {},
          "name": {"type": "string"},
          "id": {"type": "string"},
          "isStudent": {"type": "boolean"}
        }
      }
    ]
  }
}

Flattened:

{
    "id.type": "integer",
    "str.type": "string",
    "arr.type": "array",
    "arr.items[0].type": "integer",
    "arr.items[1].type": "string",
    "arr.items[2].type": "boolean",
    "arr.items[3].type": "number",
    "arr.items[4].type": "object",
    "arr.items[4].properties.empty": {},
    "arr.items[4].properties.name.type": "string",
    "arr.items[4].properties.id.type": "string",
    "arr.items[4].properties.isStudent.type": "boolean"
}
shamilnabiyev commented 4 years ago

Allowed property name characters: a-z A-Z 0-9 _