Abhijeet Kushe
Abhijeet Kushe

Reputation: 2597

Generate sample Json output from Json Schema

I want to know whether there is a method in which I can generate sample json output based on a json schema input. For example of the following input:

{
  "title": "Example Schema",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string"
    },
    "lastName": {
      "type": "string"
    },
    "age": {
      "description": "Age in years",
      "type": "integer",
      "minimum": 0
    }
  },
  "required": ["firstName", "lastName"]
}

Output:

{
  "firstName": "RandomFirstName",
  "lastName": "RandomLastName"
}

I have a large json schema with plenty of validations, so to generate a sample valid json, I could either create one manually using either Java or just a type it into a file. Is there a better way available?

Upvotes: 58

Views: 121714

Answers (6)

StingyJack
StingyJack

Reputation: 19469

Notepad++ has a plugin named "JSON Tools" that will generate a random json document for the schema file in the active document tab. There are not a lot of options, but it does work well in a pinch. enter image description here

Upvotes: 0

Clemens
Clemens

Reputation: 1817

JSONBuddy can do this for you. It is a Windows desktop JSON editor and generates live JSON sample data while you are editing your schema.

(note: I am the author)

Upvotes: 7

Eric Olson
Eric Olson

Reputation: 2985

You can try the JSON Schema Faker. It will take a schema and generate/output a JSON object that will validate against the schema.

Code available on githubcom

Upvotes: 52

Guido
Guido

Reputation: 47665

fake-schema-cli is a JavaScript option you can use.

Example: fake-schema file-input-schema.json > output.json.

Upvotes: 7

SkySoft
SkySoft

Reputation: 193

My team and I have created an online tool that allows you to parse JSON schema and generate an array of JSON data that complies to the schema. You can save it as .json file and parse it to your app with a Java parser.

The tool is called Mock turtle - https://mockturtle.net .

Upvotes: 6

Ted_Zactly
Ted_Zactly

Reputation: 167

You can also use the ModelObject in Adobe Ride (full disclosure: self-plug here). Point the ModelObject (or a subclass thereof) to a schema in your java project resources: https://github.com/adobe/ride/blob/develop/sample/sample-service-extension/src/test/java/com/adobe/ride/sample/tests/ObjectCreation.java#L38

You can also use the Ride Fuzzer Lib to easily tests sending negative data into the schema nodes (based on an array of OWASP and google injection test strings, and other various types data): https://github.com/adobe/ride/tree/develop/libraries/ride-fuzzer-lib

All Ride modules are open source and free: https://github.com/adobe/ride/

Upvotes: -1

Related Questions