DevToys-app / DevToys

A Swiss Army knife for developers.
https://devtoys.app/
MIT License
26.94k stars 1.45k forks source link

JSON Schema from JSON-Data #1375

Closed magicmarcy closed 2 months ago

magicmarcy commented 2 months ago

What feature or new tool do you think should be added to DevToys?

We already have some JSON tools in the DevToys, all of which I consider to be really useful. What I personally am still missing is a converter that creates a JSON schema from JSON data. An example of how this works can be found here: like: https://www.liquid-technologies.com/online-json-to-schema-converter

Why do you think this is needed?

I use JSON schemas primarily for testing endpoints. This allows me to verify that all properties are still being returned as expected and that no one has changed the data types or other things.

Solution/Idea

Sample-Input:

{
  "name": "Alice",
  "age": 24,
  "address": {
    "street": "Main Str.",
    "no": 12,
    "postcode": "1234"
  },
  "gender": "FEMALE"
}

Sample-Output:

{
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "age": {
      "type": "integer"
    },
    "address": {
      "type": "object",
      "properties": {
        "street": {
          "type": "string"
        },
        "no": {
          "type": "integer"
        },
        "postcode": {
          "type": "string"
        }
      },
      "additionalProperties": false,
      "required": [
        "street",
        "no",
        "postcode"
      ]
    },
    "gender": {
      "type": "string"
    }
  },
  "additionalProperties": false,
  "required": [
    "name",
    "age",
    "address",
    "gender"
  ]
}

Comments

No response

veler commented 2 months ago

HI, I wonder if this could be added to the JSON Schema extension made by @benyaa ?

benyaa commented 2 months ago

HI, I wonder if this could be added to the JSON Schema extension made by @benyaa ?

I believe it can, although I'm not sure it should be part of the Json schema validator as it is a tester and this feature is a generator. But it shouldn't take that long to make one, it would have the same GUI and use the same library to accomplish this.

veler commented 2 months ago

Indeed. I would suggest to make it a separated tool, and either in the same extension, or in a new one. If both use the same library to handle the json schema, it may be better to keep it in the same extension.

benyaa commented 2 months ago

okay, I spent the last hour implementing it and added another tool on top of it to generate classes out of json schemas. NJsonSchema is just wonderful

magicmarcy commented 2 months ago

okay, I spent the last hour implementing it and added another tools on top of it to generate classes out of json schemas. NJsonSchema is just wonderful

Wow, this is really awesome and exactly what I've been looking for! Thank you so much!

veler commented 2 months ago

okay, I spent the last hour implementing it and added another tools on top of it to generate classes out of json schemas. NJsonSchema is just wonderful

Wow wow wow that was quick! 😁👏 Thank you so much!