- The json-schema-faker module is used for creating Random JSON data from a given JSON schema.
- This library is really useful for developers and tester during development and testing.
- In this demo,”We will learn to install json-schema-faker and learn its use with a simple example”.
- This json-schema-faker library is build using faker.js, chance.js and randexp.js libraries.
- The json-schema-faker can be installed using npm install json-schema-faker –save-dev command.The following screenshot shows the terminal with json-schema-faker installation.
- To demonstrate the json-schema-faker we have created a JSONSchemaFakerDemo project.The structure of this project looks like following screenshot.
- The my-script.js file contains the code to converting a student schema t0 data.The code content of my-script.js file are as follows:-
var jsf = require('json-schema-faker'), aStudentSchema ={ type: 'object', properties: { name:{ type: 'string', faker: 'name.findName' }, score:{ type: 'integer', minimum:10, maximum:80 } } }; var aStudentData = jsf(aStudentSchema); console.log("aaStudentData: ",aStudentData);
- We can run this demo using node my-script.js command.The following screenshot shows the terminal with the output of this command with generated student data objet.
- The demo code can be downloaded from the following link:-
https://github.com/saan1984/JSONSchemaFakerDemo