'How to generate json data from Json Schema Programmatically in Java
I am trying to create Body-parameter(JSON) for my POST Api , which is a JSON request . All I have is the JSON Schema . I am trying to come up with a list of different JSON test data covering Positive and negative flows for it .
Is there any option to generate / create the JSON data programmatic using Java ? . I have attached a small Json schema (just for understanding purpose) but my actual schema is more complicated with lot of Array's and Nested Json's .
My Json Schema :
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/example.json",
"type": "object",
"title": "The Root Schema",
"description": "The root schema comprises the entire JSON document.",
"required": [
"FirstName",
"LastName",
"Age",
"Interest"
],
"properties": {
"FirstName": {
"$id": "#/properties/FirstName",
"type": "string",
"title": "The Firstname Schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"Vijay"
]
},
"LastName": {
"$id": "#/properties/LastName",
"type": "string",
"title": "The Lastname Schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"Karthik"
]
},
"Age": {
"$id": "#/properties/Age",
"type": "integer",
"title": "The Age Schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
30
]
},
"Interest": {
"$id": "#/properties/Interest",
"type": "array",
"title": "The Interest Schema",
"description": "An explanation about the purpose of this instance.",
"default": [],
"items": {
"$id": "#/properties/Interest/items",
"type": "string",
"title": "The Items Schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"Food",
"movie",
"Learning",
"VideoGames"
]
}
}
}
}enter code here
My TestData looks like :
{
"FirstName":"Vivi",
"LastName":"Karrri",
"Age":30,
"Interest":["Food","movie","Learning","VideoGames"]
}
Any suggestions how can we achive this ? Note : I am using Springboot and I have complete POJO for the request object
Solution 1:[1]
You can generate fake java-objects and then map them to JSON.
POJOs
If you already have the POJOs matching the schema, then we can skip this step. If no, to generate a POJO from the schema, for example, can be used this library: jsonschema2pojo.
Fake objects
Generating of objects with fake data can be done with a special library, some of them are listed here:
Generating JSON
It's prettry simple and can be done with Jackson:
ObjectMapper objectMapper = new ObjectMapper();
ObjectWriter prettyPrinter = objectMapper.writerWithDefaultPrettyPrinter();
String json = prettyPrinter.writeValueAsString(yourFakeObject);
Solution 2:[2]
If you have json schema then you can directly generate a sample JSON message from it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ilya Lysenko |
Solution 2 | Jignesh Dhua |