
Greetings
I am a JavaScript developer and have recently been developing Ext JS applications. For clarity of interfaces, it is sometimes necessary to drive JSON data into them. At first I wrote small PHP scripts to generate the necessary data, but soon it began to take more and more time and I abandoned this approach. Then I started creating * .json data files, but often I needed to have fields with a unique ID and, if possible, unique data for beauty. True, setting unique values for fields, especially in an array of 100+ objects, is not a gratifying task.
And I decided to automate this process a bit.
')
I often use the service
Json Parser Online . It is extremely useful in parsing the response from the server and formats JSON into a beautiful tree. But, alas, can not generate it. Inspired by this project and tired of the routine fuss, in a couple of days I wrote a small service
Json Generator to generate JSON.
Implementation
In appearance, it is similar to Json Parser - this design was painfully convenient for me. The service is written in pure javascript. Only one third-party library is used -
JSON-js from Douglas Crockford. There are two fields on the page: the template field of the cloned object and the field for the generated data, which are represented as a tree. The JSON template field must be valid. The generated data is enclosed in this object:
{ "id": 1, "jsonrpc": "2.0", "total": 5,
The script understands several variables:
- int $ incr (account from 1)
- int $ randInt (a, b) (random integer in the range from a to b)
- boolean $ bool (random true / false)
- string $ firstName / $ lastName (random Russian name / surname. At the moment there are 100 randomly selected)
- string $ company (random company name out of 100)
- string $ phone (random Russian phone number)
- string $ email (random email address. Generated depending on the field values of the object containing $ name and $ company)
- string $ date (random date)
- array field name (the number of array elements is controlled by the variable repeat: int count )
The template can be supplemented with any fields with any content - they will remain unchanged. The generator understands any level of nesting of objects - for all fields of nested objects containing variables, new data will be generated.
The toolbar in the middle consists of:
- field to enter the length of the generated array
- generation start buttons
- field reset buttons
- buttons to highlight text in the field with new data
PS
The project is still damp, but for me it saves time well. In the future I plan to improve it and add new buns. I hope that someone will find this service useful for themselves. Thanks for attention!
UPD
Added the ability to generate arrays of objects. Their size is controlled by the variable
repeat: int count . All fields in the array object may contain other variables. An example can be seen in the new template. Thank you very much
taliban for the tip :)