mockJson is a Go library for quickly generating mock JSON data based on custom templates. It is suitable for API debugging, frontend development, automated testing, and other scenarios. It supports rich data types and flexible template syntax, making it easy for developers to customize data structures and content.
- Generate structured mock data from template strings
- Built-in rich mock data types (such as name, address, phone number, email, date, color, number, etc.)
- Support for custom mock rules and parameters
- Support for nested objects, arrays, random quantity, and other complex structures
- Easily extensible, supports custom mock functions
go get github.com/zjhsd2007/mockJson
Or directly place the project source code into your project.
import "mockJson"
The template is a JSON string that supports special mock tags (starting with @), for example:
template := `{
"id": "@Id",
"name": "@Name|EN",
"age": "@Number|18~60",
"email": "@Email",
"address": "@Address",
"tags": ["@Color", 2, 4], // Generate 2~4 colors
"created_at": "@DateTime"
}`
result := mockJson.Mock(template)
fmt.Println(string(result))
Example output:
{
"id": "c8f2e1a2-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"name": "Zhang San",
"age": 25,
"email": "[email protected]",
"address": "Beijing Chaoyang District xxx",
"tags": ["#FF0000", "#00FF00", "#0000FF"],
"created_at": "2024-06-01 12:34:56"
}
- Strings starting with @ are mock tags, supporting parameters (separated by |)
- Supports nested objects and arrays
- Array syntax like
["@Name", 2, 5]
means generating 2~5 random names - With parameters like
@Float|10~20
means generating a random float number between 10 and 20
Tag | Description | Optional Parameters | Optional Parameters |
---|---|---|---|
@Guid | GUID/UUID | None | |
@Id | Random ID | Length | ID length |
@IdNumber | ID Card Number | None | |
@Phone | Phone Number | Prefix | |
@Bool | Boolean | None | |
None | |||
@Ip | IP Address | ipv4/ipv6 | |
@Url | URL | Prefix | protocol |
@Image | Image URL | bgColor/fgColor | |
@Token | Token String | None | |
@Domain | Domain | None | |
@Number | Integer | min~max | |
@Float | Float | min~max/precision | |
@Sentence | Sentence | CN/EN | default: CN |
@Paragraph | Paragraph | CN/EN | default: CN |
@Title | Title | CN/EN | default: CN |
@Date | Date | None | |
@DateTime | DateTime | None | |
@Time | Time | None | |
@Timestamp | Timestamp | None | |
@Rgb | RGB Color | None | |
@Hsl | HSL Color | None | |
@Rgba | RGBA Color | None | |
@Color | Color | None | Hex Color |
@FirstName | First Name | CN/EN | default: CN |
@LastName | Last Name | CN/EN | default: CN |
@Name | Full Name | CN/EN | default: CN |
@Zip | Zip Code | None | |
@Address | Address | CN/EN | default: CN |
You can register your own mock generator function via the Register method:
mockJson.Register("@MyType", func(args ...any) any {
// Custom generation logic
return "custom content"
})
template := `{
"list": ["@Name", 3, 7]
}`
Generates an array of 3~7 random names.
template := `{
"user": {
"id": "@Id",
"profile": {
"name": "@Name",
"email": "@Email"
}
}
}`
template := `{
"id": "@Id",
"id2": "@Id|5",
"phone1": "@Phone",
"phone2": "@Phone|86",
"ip":"@Ip",
"ipv6":"@Ip|ipv6",
"url": "@Url",
"url2":"@Url|ftp',
"image": "@Image",
"image1": "@Image|200X300",
"image2": "@Image|200X300|#000|#fff",
"name":"@Name",
"name2":"@Name|EN",
"firstName":"@FirstName",
"firstName2":"@FirstName|EN",
"lastName":"@LastName",
"lastName2":"@LastName|EN",
"number":"@Number",
"number1":"@Number|10-100",
"float": "@Float|3", // generate a float number with 3 decimal
"float2": "@Float|10~20" // generate a float number between 10~20
"float3": "@Float|3|1~10" // generate a float number with 3 decimal between 1~10
"address": "@Address"
"address2": "@Address|EN"
}`
If the template format is incorrect or mock fails, the Mock method will return nil and print an error message to the console.
Feel free to submit issues or PRs to add more mock types and features!
MIT
For more detailed API documentation or special requirements, please let me know!