简体   繁体   中英

How do I convert JSON to a JSON schema programmatically in Java?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM