1

I'm trying to get all of the keys from this JSON schema using react.js. The end goal is to create a system that will automatically create a dynamic form based on this schema that will change during the projects lifetime.

{
  "$comment": "If additional properties are needed, then the schema should be updated",
  "$id": "http://example.com/topologySchema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "additionalProperties": false,
  "description": "This entry is for topology information about an architected material",
  "definitions": {
    "network": {
      "description": "Defines objects for architected materials that consist of a network of building blocks that are perfectly bonded together",
      "type": "object",
      "properties": {
        "nodes": {
          "type": "array"
        },
        "connectivity": {
          "type": "array"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "family": {
                "const": "network"
              }
            }
          },
          "then": {
            "properties": {
              "nodes": {
                "type": "array"
              },
              "connectivity": {
                "type": "array"
              }
            },
            "required": [
              "nodes",
              "connectivity"
            ]
          }
        }
      ],
      "additionalProperties": false
    },
    "setDimension": {
      "description": "Takes parameter 'architecture_dimensionality' and associates appropriate dimensionality for parameter 'nodes' Does the same for the properties 'building_block_dimensionality' and 'connectivity'",
      "allOf": [
        {
          "if": {
            "properties": {
              "architecture_dimensionality": {
                "const": "1D"
              }
            }
          },
          "then": {
            "properties": {
              "nodes": {
                "type": "array",
                "minItems": 1,
                "items": {
                  "type": "array",
                  "minItems": 1,
                  "maxItems": 1,
                  "items": {
                    "type": "number"
                  }
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "architecture_dimensionality": {
                "const": "2D"
              }
            }
          },
          "then": {
            "properties": {
              "nodes": {
                "type": "array",
                "minItems": 1,
                "items": {
                  "type": "array",
                  "minItems": 2,
                  "maxItems": 2,
                  "items": {
                    "type": "number"
                  }
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "architecture_dimensionality": {
                "const": "2.5D"
              }
            }
          },
          "then": {
            "properties": {
              "nodes": {
                "type": "array",
                "minItems": 1,
                "items": {
                  "type": "array",
                  "minItems": 2,
                  "maxItems": 2,
                  "items": {
                    "type": "number"
                  }
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "architecture_dimensionality": {
                "const": "3D"
              }
            }
          },
          "then": {
            "properties": {
              "nodes": {
                "type": "array",
                "minItems": 1,
                "items": {
                  "type": "array",
                  "minItems": 3,
                  "maxItems": 3,
                  "items": {
                    "type": "number"
                  }
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "building_block_dimensionality": {
                "const": "1D"
              }
            }
          },
          "then": {
            "properties": {
              "connectivity": {
                "type": "array",
                "minItems": 1,
                "items": {
                  "type": "array",
                  "minItems": 2,
                  "maxItems": 2,
                  "items": {
                    "type": "number"
                  }
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "building_block_dimensionality": {
                "const": "2D"
              }
            }
          },
          "then": {
            "properties": {
              "connectivity": {
                "type": "array",
                "minItems": 1,
                "items": {
                  "type": "array",
                  "minItems": 3,
                  "items": {
                    "type": "number"
                  }
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "building_block_dimensionality": {
                "const": "2.5D"
              }
            }
          },
          "then": {
            "properties": {
              "connectivity": {
                "type": "array",
                "minItems": 1,
                "items": {
                  "type": "array",
                  "minItems": 3,
                  "items": {
                    "type": "number"
                  }
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "building_block_dimensionality": {
                "const": "3D"
              }
            }
          },
          "then": {
            "properties": {
              "connectivity": {
                "type": "array",
                "minItems": 1,
                "items": {
                  "type": "array",
                  "minItems": 4,
                  "items": {
                    "type": "number"
                  }
                }
              }
            }
          }
        }
      ]
    },
    "topology": {
      "type": "object",
      "properties": {
        "architecture_dimensionality": {
          "title": "architecture_dimensionality",
          "type": "string",
          "enum": [
            "1D",
            "2D",
            "2.5D",
            "3D"
          ]
        },
        "building_block_dimensionality": {
          "type": "string",
          "enum": [
            "1D",
            "2D",
            "2.5D",
            "3D"
          ]
        },
        "family": {
          "type": "string",
          "enum": [
            "network",
            "kirigami",
            "origami",
            "linkage",
            "IPC",
            "sandwich",
            "other"
          ]
        },
        "hierarchicalParent": {
          "type": "boolean"
        },
        "child": {
          "type": "object",
          "$ref": "#/definitions/topology"
        }
      },
      "allOf": [
        {
          "$ref": "#/definitions/setDimension"
        },
        {
          "$ref": "#/definitions/network"
        },
        {
          "if": {
            "properties": {
              "hierarchicalParent": {
                "const": false
              }
            }
          },
          "then": {
            "properties": {
              "child": {
                "not": {}
              }
            }
          },
          "else": {
            "required": ["child"]
          }
        }
      ]
    }
  },
  "properties": {
    "topology": { "$ref": "#/definitions/topology" }
  },

  "title": "Topology",
  "type": "object"
}

I've tried several different ways of attempting to parse the entire schema for just the keys at first and I'm struggling to get anything to work.

iRazed
  • 11
  • 1
  • i cant comment but Possible duplicate Is [here](https://stackoverflow.com/questions/44796986/extract-keys-of-json-schema-without-building-json-data) – L8R Mar 31 '22 at 22:35
  • I thought so too but, it doesn't seem to be working with this schema. – iRazed Mar 31 '22 at 23:47
  • 1
    "can't get anything to work" is vague. What approaches have you tried? What's working, and what's not working? But.. you're attempting to reinvent the wheel (think about it -- do you think you're the first person who has wanted to do this?) There are already form generators for json schema in React. https://json-schema.org/implementations.html#web-ui-generation – Ether Apr 01 '22 at 00:27
  • Second to what @Ether says. Doing this is far more complex than you might expect. Even the existing solutions, with multiple months of development put in, do not cover all situations. Some solutions cover most though. – Relequestual Apr 01 '22 at 09:51

0 Answers0