如何在Java中通过编程方式将JSON转换为JSON Schema?
JSON转JSON Schema的Java实现方案
示例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" ] }
问题
如何使用Java将JSON转换为JSON Schema?需要用到哪些内置函数?我不想使用独立工具,希望在Java程序运行时生成。
核心说明
JDK本身没有提供直接将JSON转换为JSON Schema的内置函数,你需要自行编写解析逻辑,或借助常用的JSON处理库(如Jackson、Gson)辅助实现——这些库属于可集成的依赖包,并非独立工具,能满足运行时生成的需求。
基于Jackson库的实现示例
Jackson是Java生态中常用的JSON处理库,下面是用它实现JSON转Schema的代码:
- 先添加Maven依赖(若为Gradle项目可对应调整):
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.2</version> </dependency>
- 实现转换逻辑:
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; public class JsonToSchemaGenerator { private static final ObjectMapper mapper = new ObjectMapper(); public static JsonNode generateSchema(String jsonStr) throws Exception { JsonNode rootNode = mapper.readTree(jsonStr); ObjectNode schema = mapper.createObjectNode(); schema.put("$schema", "http://json-schema.org/draft-04/schema#"); buildSchema(rootNode, schema); return schema; } private static void buildSchema(JsonNode node, ObjectNode schema) { if (node.isObject()) { schema.put("type", "object"); ObjectNode properties = mapper.createObjectNode(); List<String> requiredFields = new ArrayList<>(); Iterator<Map.Entry<String, JsonNode>> fields = node.fields(); while (fields.hasNext()) { Map.Entry<String, JsonNode> field = fields.next(); String fieldName = field.getKey(); JsonNode fieldValue = field.getValue(); ObjectNode propSchema = mapper.createObjectNode(); buildSchema(fieldValue, propSchema); properties.set(fieldName, propSchema); requiredFields.add(fieldName); } schema.set("properties", properties); schema.putPOJO("required", requiredFields); } else if (node.isArray()) { schema.put("type", "array"); if (!node.isEmpty()) { JsonNode firstItem = node.get(0); ObjectNode itemSchema = mapper.createObjectNode(); buildSchema(firstItem, itemSchema); schema.set("items", itemSchema); } } else if (node.isTextual()) { schema.put("type", "string"); } else if (node.isInt() || node.isLong()) { schema.put("type", "integer"); } else if (node.isFloat() || node.isDouble()) { schema.put("type", "number"); } else if (node.isBoolean()) { schema.put("type", "boolean"); } else if (node.isNull()) { schema.put("type", "null"); } } public static void main(String[] args) throws Exception { String inputJson = "{\"id\":1,\"name\":\"abc\",\"tech\":\"java\"}"; JsonNode schema = generateSchema(inputJson); System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema)); } }
代码说明
- 代码通过Jackson的
JsonNode遍历JSON结构,根据节点类型(对象、数组、字符串等)生成对应的Schema字段; - 默认将所有字段标记为必填,若不需要可删除
requiredFields相关逻辑; - 若不想引入第三方库,也可使用Java 11+内置的
javax.json包实现,但解析和节点操作会更繁琐。
内容的提问来源于stack exchange,提问作者Chethan
相关产品推荐
相关产品推荐

