Json random generator is a tool to produce data from json schema automatically, even nested lists are supported! Written fully in Scala, uses POJOs generated by http://www.jsonschema2pojo.org.
- Place your schema in: src/main/resources/schema.
- Run
mvn generate-sources
to obtain object oriented representation of your json schema. Generated POJOs will be placed in target directory. - Set SchemaType in
Config.scala
to POJO type that you are interested in. - Set schemaPath in
Config.scala
. It's path to generated POJO: output.<path_to_POJO>.<POJO_Class_Name> - Set numOfRecords in
Config.scala
. - Run
Main.scala
. Generated json will be in target directory. - (Optional) If you are not pleased with default generated values in json, you can always overwrite it, using customRules method in
Config.scala
file and runMain.scala
again.
object Config {
type SchemaType = AnyRef /* YOUR SCHEMA TYPE HERE (generated POJO type), e.g. ComplexSchema */
def schemaPath = "" //e.g. output.ComplexSchema
def numOfRecords = 10
def customRules: Seq[SchemaType] => Seq[SchemaType] =
//You can define here additional rules for specific fields
//Example:
objects => Mapping(objects) { rows =>
rows.zipWithIndex.map { case (r, idx) =>
//set date-time between range of dates
r.setCreatedDateTime(
DateTypeGenerators.between(new Date(1581809593000L), new Date(1582809593000L)).sample.get
)
//set date the same as createdDateTime
r.setCreatedDate(DateTimeUtils.date2Date(r.getCreatedDateTime))
//set time the same as createdDateTime
r.setCreatedTime(DateTimeUtils.date2Time(r.getCreatedDateTime))
//set flatNo between 1000 and 10000 with precision 7 and scale 2
r.getBillingAddress.setFlatNo(BigDecimalTypeGenerators.between(1000, 10000, 7, 2).sample.get)
//set id of the user with increasing number
r.getPerson.setId(idx.toLong)
//set name using generator
r.getPerson.setName(Gen.oneOf("Gabriel", "Alicja", "Rafal", "Vova", "Milton", "Pawel").sample.get)
//set lastname using generator
r.getPerson.setLastname(Gen.oneOf("Kowalski", "Smith", "Brown", "Wilson", "Miller", "Johnson").sample.get)
}
}
}
- Place your schema somewhere on the disk e.g. /c/schema
- Run:
mvn clean package -Dschema.dir=/c/schema
to build the project with generated object oriented representation of your json schema - (Optional) If you want to check what classes have been generated, you can use this command:
jar tf target/json-random-generator-1.0-SNAPSHOT.jar | grep ^output*
All the classes are generated in output folder.
- Run:
java -jar target/json-random-generator-1.0-SNAPSHOT.jar output.<path_to_POJO>.<POJO_Class_Name> <num_of_events> <output_dir>
Json random generator supports mocking data by using annotations that was developed in this project: https://github.com/kklimexk/jsonschema2pojo/tree/develop. If you want to use these annotations, integration between projects is needed, because the modified jsonschema2pojo is not in the central maven repository. Basically we need to modify pom.xml
in json random generator to use jsonschema2pojo in version 1.0.3-SNAPSHOT
. Currently supported annotations in generator:
- ValueHintDecimal
- ValueHintOptions
- ValueHintIterator
- ValueHintPrefix
- ValueHintPostfix
- ValueHintRange
These annotations are generated in the POJOs when user defines jrg.properties
for schema to control generating fields e.g.
{
"$schema": "http://json-schema.org/draft-06/schema#",
"definitions": {
"address": {
"type": "object",
"properties": {
"street_address": { "type": "string" },
"city": { "type": "string" , "jrg.properties" : {
"options" : [
"London",
"New York",
"Tokyo"],
"postfix" :"_city"
}},
"state": { "type": "string" ,"jrg.properties" : {
"regex" : "^[2-9]\\d{2}-\\d{3}-\\d{4}$",
"length" : {
"min" : 4,
"max" : 10
}
}},
"buildingNo": { "type": "integer" },
"flatNo": { "type": "number", "jrg.properties" : {
"iterator" : {
"start": 1,
"restart": 200,
"step" : 5,
"initial" :100
},
"prefix" :"LN"
}},
"isTaken": { "type": "boolean" },
"credit": { "type": "number","jrg.properties" : {
"decimal" : {
"precision": 11,
"scale": 4
},
"range" : {
"min" : 3,
"max" : 44
}
}},
"info": { "type": "null" },
"orderReleaseDate": {
"type": "string",
"format": "date"
},
"orderReleaseDateTime": {
"type": "string",
"format": "date-time"
},
"orderReleaseTime": {
"type": "string",
"format": "time"
},
"typeOfAddress": { "enum": ["normal", "special"] }
},
"required": ["street_address", "city", "state"]
},
"numbersArr": {
"type": "array", "jrg.properties" : {
"decimal" : {
"precision": 11,
"scale": 4
}
},
"items": {
"type": "number"
}}
},
"type": "object",
"properties": {
"billing_address": { "$ref": "#/definitions/address" },
"shipping_address": {
"allOf": [
{ "$ref": "#/definitions/address" },
{ "properties":
{ "type": { "enum": [ "residential", "business" ] } },
"required": ["type"]
}
]
},
"numbersArray": { "$ref": "#/definitions/numbersArr" },
"stringField": { "type": "string" },
"coolMap": {
"type": "object",
"additionalProperties": { "type": "string" }
},
"stringArray": {
"type": "array",
"items": {
"type": "string"
}
},
"integerArray": {
"type": "array",
"items": {
"type": "integer"
}
},
"doubleArray": {
"type": "array",
"items": {
"type": "number"
}
},
"booleanArray": {
"type": "array",
"items": {
"type": "boolean"
}
},
"addressArray": {
"type": "array",
"items": { "$ref": "#/definitions/address" }
},
"integerNestedArray": {
"type": "array",
"items": {
"type": "array",
"items": {
"type": "integer"
}
}
},
"addressNestedArray": {
"type": "array",
"items": {
"type": "array",
"items": {
"type": "array",
"items": { "$ref": "#/definitions/address" }
}
}
}
}
}
Schema type | Java type |
---|---|
string |
java.lang.String |
integer |
java.lang.Long |
number |
java.math.BigDecimal |
boolean |
java.lang.Boolean |
null |
java.lang.Object |
enum |
generated Java enum |
object |
generated Java type |
map<string,string> |
java.util.Map<String,String> |
array |
java.util.List<String or Integer or JavaType etc.> |
array<enum> |
java.util.List<EnumType> |
nested array |
java.util.List<java.util.List<JavaType or another List>> |
date-time |
java.util.Date in ISO 8601 format |
date |
java.lang.String |
time |
java.lang.String |