-1

This is the JSON:

{"id":1,"name":"abc","tech":"java"}

JSON Schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "id": {
      "type": "integer"
    },
    "name": {
      "type": "string"
    },
    "tech": {
      "type": "string"
    }
  },
  "required": [
    "id",
    "name",
    "tech"
  ]
}

How do I convert JSON to JSON Schema using Java?

What are the inbuilt functions I need to use here? My goal is to not use a standalone tool because I want to generate it on runtime using Java in program.

Ivar
  • 6,138
  • 12
  • 49
  • 61
Chethan
  • 1
  • 1
  • Does this answer your question? [Generate sample Json output from Json Schema](https://stackoverflow.com/questions/21894873/generate-sample-json-output-from-json-schema) – lavantho0508 Nov 24 '22 at 08:31
  • sorry..the above is Json Scehma to Json, but i'm looking to implement json to json schema... – Chethan Nov 24 '22 at 08:42

1 Answers1

1

This is the only correct answer: wrap your data in {"const": ... }.

Otherwise, you are asking a program to guess what things in your data are going to remain constant and which will vary, and that's impossible with only one sample.

Maybe with many (hundreds or thousands?) of data samples you could have a program generate a JSON Schema that validated all of them, but it's still just guesswork. Ultimately it comes down to you, a human being, to express the context of your data in the form of a schema.

Ether
  • 53,118
  • 13
  • 86
  • 159