1

Can anyone help me to create the code to generate Sample JSON using JSON Schema in javascript. I have a JSON Schema as input and I have to get JSON as output after javascript transformation.

The code should be dynamic.

example:

JSON SCHEMA--

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "glossary": {
            "type": "object",
            "properties": {
                "title": {
                    "type": "string"
                },
                "GlossDiv": {
                    "type": "object",
                    "properties": {
                        "title": {
                            "type": "string"
                        },
                        "GlossList": {
                            "type": "object",
                            "properties": {
                                "GlossEntry": {
                                    "type": "object",
                                    "properties": {
                                        "ID": {
                                            "type": "string"
                                        },
                                        "SortAs": {
                                            "type": "string"
                                        },
                                        "GlossTerm": {
                                            "type": "string"
                                        },
                                        "Acronym": {
                                            "type": "string"
                                        },
                                        "Abbrev": {
                                            "type": "string"
                                        },
                                        "GlossDef": {
                                            "type": "object",
                                            "properties": {
                                                "para": {
                                                    "type": "string"
                                                },
                                                "GlossSeeAlso": {
                                                    "type": "array",
                                                    "items": [
                                                        {
                                                            "type": "string"
                                                        }, {
                                                            "type": "string"
                                                        }
                                                    ]
                                                }
                                            },
                                            "required": [
                                                "para", "GlossSeeAlso"
                                            ]
                                        },
                                        "GlossSee": {
                                            "type": "string"
                                        }
                                    },
                                    "required": [
                                        "ID", "SortAs", "GlossTerm", "Acronym", "Abbrev", "GlossDef", "GlossSee"
                                    ]
                                }
                            },
                            "required": [
                                "GlossEntry"
                            ]
                        }
                    },
                    "required": [
                        "title", "GlossList"
                    ]
                }
            },
            "required": [
                "title", "GlossDiv"
            ]
        }
    },
    "required": [
        "glossary"
    ]
}

JSON OUTPUT--

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": [
                            "GML", "XML"
                        ]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}
Theo
  • 57,719
  • 8
  • 24
  • 41
Gunjan
  • 21
  • 1
  • 2
  • 1
    What have you tried so far? – Derek Pollard Dec 04 '18 at 15:49
  • i want to create JSON based on JSON Schema. – Gunjan Dec 04 '18 at 15:50
  • The best way to get help here is to first try something. If you get stuck, do some research, make an attempt at fixing things yourself, and only then ask a specific question about your attempts, showing what you have tried. Questions asking for complete solutions without demonstrating research effort usually get downvoted and closed. – Derek Pollard Dec 04 '18 at 15:55
  • I already research that's why i asked this question. If cant help, please suggest the place where i can find the solution. – Gunjan Dec 04 '18 at 16:05
  • Can anyone help please – Gunjan Dec 08 '18 at 10:24
  • The problem is you haven't tried to solve the problem yourself. We don't provide ground up solutions. You should attempt to solve it yourself with javascript (or any other language), and if you truly get stuck, come back here. – Derek Pollard Dec 08 '18 at 19:38

1 Answers1

0

There is an open source generator of fake data from JSON Schema. You can use this project as a startpoint, I guess:

https://json-schema-faker.js.org/