61

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?

7
  • Do you want a bespoke solution written in Java, to write output to a file? What have you tried already? Commented Feb 19, 2014 at 23:46
  • Preferably I wanted a java library but I am would take any format form a standalone script to an online tool. I have not yet started writing my own solution problem.I thought better to check if there is some existing library which does that Commented Feb 20, 2014 at 3:19
  • do we have the .NET or .NET CORE solution for this purpose? Commented Sep 9, 2020 at 6:30
  • @AbhijeetKushe have you found any java library capable to do that Commented Feb 2, 2021 at 6:35
  • No i did not.This was a long time back.It can be scripted but I realized later on it is not worth it.It is a good hackathon project but wont be useful for unit testing.It is always better to have a clear expected output and input as tests serve as documentation.For integration test we can always using templating language like freemarker or Velocity to achieve what we want.Java never added string interpolation like Groovy, scala so 3rd libraries would be needed Commented Feb 2, 2021 at 18:27

6 Answers 6

54

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

Sign up to request clarification or add additional context in comments.

3 Comments

Eric I am looking for any java library for the same json schema to json object convertion.
I am not familiar with any @VishwaPratap. Good luck!
@VishwaPratap have you found any alternatives
7

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 .

3 Comments

nice tool! Thanks a lot.
Great tool man, I can't stop using it!
Is it open source? If so can you please give a GitHub link?
7

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

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

Comments

7

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)

Comments

0

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

Comments

-1

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.