In front-end development, when doing the front-end separation, often need to write JSON data, there are 2 problems particularly worried:
1, the data is written dead, can not be randomly generated according to certain conditions of different length, different content of data
2, when writing an array, if there are many bars, need to write a piece of writing, time-consuming and laborious
Randomjson is especially useful for generating json,mock JSON data randomly from the model.
GitHub Address:
Https://github.com/finance-sh/randomjson
How to use the installation
NPM Install Randomjson
Usage
Reference Packagevar Randomjson=Require' Randomjson‘);Model Modeljson= {"RET":"200","Msg":"< @string {1,10}|number>","P1":"< @null >","Info":"< @chinese >","P2":"< @boolean >","P7": {"P11<@{2,5}>": [ {"P8":"12312","P9": ["< @string {4,7}>"],"P10":"< @string >" } ] },"Data<@{1,10}>": [ {"P8":"12312","P9":7777,"P10":"< @string >" } ],"P15":"< @number {4}>-< @number {2}>-< @number {2}>" }Generate JSON based on modelvar Myjson=Randomjson (Modeljson); The JSON generated based on the top model may be: {"RET":1708,"Msg":"This Ec","P1":Null"Info":"The sixth edition of the centralized development began in 2","P2":True"P7": {"P11": [{"P8":"12312","P9": ["This"],"P10":"This ECMA Standa" }, {"P8":"12312","P9": ["This"],"P10":"This ECMA Standa" }, {"P8":"12312","P9": ["This E"],"P10":"This" }] },The data": [{"P8":"12312","P9":7777,"P10":The This ECMA St" }, {"P8":"12312","P9":7777,"P10":"This ECMA standard D" }, {"P8":"12312","P9":7777,"P10":"This ECMA standard D" }, {"P8":"12312","P9":7777,"P10":The This ECMA" }, {"P8":"12312","P9":7777,"P10": "This ECMA standa"}, { "P8": "12312", "P9": 7777, " p10": "This E"}], "P15": "8302-82-22"} /c25>
Lexical
Start with <@, End with >, middle content is Randomjson's lexical
Model Definition Data type
"< @number >": Number Type
"< @string >": String type
"< @null >": null
"< @boolean >": Boolean type
"< @chinese >": Chinese Strings
Quantifiers
{Minnumber, MaxNumber}: Minnumber to MaxNumber
{minnumber,}: Minimum Minnumber
{Number}: Number of
Randomly generate one of them
你可以使用“|” 要分离多个选项,随机值是其中之一
Quantifier Definition Note points
When the value is non-array, the length is in the value string, for example: "ret": "< @number {4}>"
When the value is an array, the length is in key, for example:
"P11<@{2,5}>": [{ "P8""12312" P9": [" < @string {4,7}>" P10"" < @string >"}]
The project is continuing to develop.
GitHub Address:
Https://github.com/finance-sh/randomjson
Generating a J random JSON package by condition: Randomjson