50

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 :-

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 ?

Abhijeet Kushe
  • 2,477
  • 3
  • 26
  • 39
  • Do you want a bespoke solution written in Java, to write output to a file? What have you tried already? – Nick Grealy Feb 19 '14 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 – Abhijeet Kushe Feb 20 '14 at 03:19
  • do we have the `.NET` or `.NET CORE` solution for this purpose? – Shawn Sep 09 '20 at 06:30
  • @AbhijeetKushe have you found any java library capable to do that – pacman Feb 02 '21 at 06: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 – Abhijeet Kushe Feb 02 '21 at 18:27
  • Actually now that I think of it u can invoke fake-schema cli as a command from java and get its output.It will obviously not be performant as u will be spawning a new process each.You can try running nodejs fake schema in Nashorn and see if you can generate the output – Abhijeet Kushe Feb 02 '21 at 18:32
  • Does this answer your question? [Tool to generate JSON schema from JSON data](https://stackoverflow.com/questions/7341537/tool-to-generate-json-schema-from-json-data) – dippas Jul 22 '21 at 12:54

5 Answers5

40

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

Eric Olson
  • 2,827
  • 1
  • 20
  • 21
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.

Clemens
  • 1,744
  • 11
  • 20
5

fake-schema-cli is another option you can use.

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

Guido
  • 46,642
  • 28
  • 120
  • 174
3

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 .

Dharman
  • 30,962
  • 25
  • 85
  • 135
SkySoft
  • 133
  • 7
-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/

Ted_Zactly
  • 167
  • 1
  • 3