SoFunction
Updated on 2025-04-10

js generate random json:randomjson implementation method

In front-end development, when separating front-end, you often need to hand-written json data. There are three problems that are particularly worrying:

1. The data is written dead, and data with different lengths and contents cannot be generated randomly according to certain conditions.

2. When writing an array, if there are many pieces, it needs to be written one by one, which is time-consuming and labor-intensive.

3. Mock pictures are particularly difficult

Randomjson is particularly useful when randomly generating json and mock json data based on the model.

Github address:

/finance-sh/randomjson

How to use

# randomjson

Generate a random json corresponding to json based on the conditions

## How to use

### Install

   npm install randomjson

### Usage

// Install
  npm install randomjson

// Usage  // Quote package  var randomjson = require('randomjson');
  // Model  var modelJson = {
    "code": "00",
    "numberCode": "<@[10000,20000]>",
    "msg": "<@chinese{12,50}>",
    "msg2": "<@string{2,3}>",
    "logo": "<@image{100,100}>",
    "result": {
      "pList<@{1,3}>":[ 
        {
          "indexToString": "<@index><@>",
          "index": "<@index>",
          "id": "<@[1-5]>", 
          "price": "<@float>",
          "name": "Company Name<@index>",
          "person": "Li Wenwu<@index>",
          "address": "Xi Sanqi, Haidian District, Beijing",
          "mobile": "1&lt;@number{10}&gt;",
          "tel": "&lt;@number{4}&gt;-&lt;@number{8}&gt;",
          "list": [
            {
              "auditKey": 1,
              "auditValue": "&lt;@[0,1,2]&gt;"
            }, 
            {
              "auditKey": 2,
              "auditValue": "&lt;@[0,1,2]&gt;"
            }
          ]
        }
      ]
    }
  }
  // Generate json based on the model  var myJson = randomjson(modelJson);
  
  Generated from the above modeljsonMaybe that's it:
  
  {
    "code": "00",
    "numberCode": 10000,
    "msg": "Add to the meaning of the border and suppress you to cure the radical question or the special agricultural materials",
    "msg2": "dv",
    "logo": "/100x100",
    "result": {
      "pList": [
        {
          "indexToString": "1",
          "index": 1,
          "id": 2,
          "price": 93.78,
          "name": "Company Name 1",
          "person": "Li Wenwu 1",
          "address": "Xi Sanqi, Haidian District, Beijing",
          "mobile": "17929438781",
          "tel": "1148-56055642",
          "list": [
            {
              "auditKey": 1,
              "auditValue": 0
            },
            {
              "auditKey": 2,
              "auditValue": 1
            }
          ]
        },
        {
          "indexToString": "2",
          "index": 2,
          "id": 5,
          "price": 29.49,
          "name": "Company Name 2",
          "person": "Li Wenwu 2",
          "address": "Xi Sanqi, Haidian District, Beijing",
          "mobile": "17826945504",
          "tel": "7298-46226026",
          "list": [
            {
              "auditKey": 1,
              "auditValue": 0
            },
            {
              "auditKey": 2,
              "auditValue": 0
            }
          ]
        }
      ]
    }
  }

## lexical method

Start with <@, end with >, the middle content is the word "randomjson"

## Model definition

### Data Type

"<@number>": number type

"<@string>": string type

   "<@null>": null

"<@boolean>": boolean type

"<@chinese>": Chinese string

"<@float{number1,number2}>": Float number number1 represents integer digits, number2 represents decimal digits, and default is 2
  
"<@image{width,height}>": The width of the generated image is the width of the generated image, height is the height of the generated image, the default is 400,400

"<@index>": The position of the element in the array, starting from 1

### Quantitative Words

{minNumber, maxNumber}: minNumber to maxNumber

{minNumber,}: Minimum minNumber

{number}: number

If it is float, minNumber, maxNumber represents the number of bits of the integer part and the decimal part.

If it is imgae, minNumber, maxNumber indicates the image width and height
  
### Randomly generate one of them

You can use "|" to separate multiple options, with a random value being one of them, and the value being a string

### Digital range

[1,2,3] represents one of 1,2,3, the value is a number

[1-90] represents an int value from 1 to 90, the value is a number

### Number to string

Placeholder that adds empty after the number type
 
"indexToString": "<@index><@>"

### Quantitle definition points

When the value is not an array, the length is in the value string, for example: "ret": "<@number{4}>"

When the value is an array, the length is in the key, for example:

"p11<@{2,5}>": [{
        "p8": "12312",
        "p9": ["<@string{4,7}>"],
        "p10": "<@string>"
      }]

Github address:

/finance-sh/randomjson

The above article js generates random json:randomjson implementation method is all the content I share with you. I hope you can give you a reference and I hope you support me more.