57

Take the following #definition from the pet store example. Given a #definition section a JSON structure can be generated

e.g. Coversion of definition to JSON

Is there something that can do the reverse given a largeish complex JSON file?

Given the below JSON Structure can I get the #defintion section of a swagger file generated to save some typing

{
  "variable": "sample",
  "object1": {
    "obj-field1": "field 1 of object",
    "obj-field2": "field 2 of object",
    "anArray": [
      "Value 1",
      {
        "anArrayObj1": "obj1fieldinarray",
        "anArrayObj2": "obj2fieldinarray"
      }
    ]
  }
}
user1605665
  • 3,771
  • 10
  • 36
  • 54
  • Possible duplicate of [Convert JSON to JSON Schema draft 4 compatible with Swagger 2.0](https://stackoverflow.com/questions/40908214/convert-json-to-json-schema-draft-4-compatible-with-swagger-2-0) – Helen Sep 15 '18 at 09:20
  • Possible duplicate of [Can I create a stub swagger model definition from a json file](https://stackoverflow.com/questions/36774452/can-i-create-a-stub-swagger-model-definition-from-a-json-file) – Helen Oct 15 '18 at 16:39
  • Does this answer your question? [Conversion of swagger json specification to the static html file in .NET/C#](https://stackoverflow.com/questions/33395223/conversion-of-swagger-json-specification-to-the-static-html-file-in-net-c) – Prany Jan 14 '20 at 17:10

4 Answers4

88

You can use this JSON-to-OpenAPI schema converter:
https://roger13.github.io/SwagDefGen/

(GitHub project)

I haven't used it personally though, so I'm not sure how good it is.


Since OpenAPI uses a subset of JSON Schema, you could also use one of the JSON Schema generators, however you may need to manually tweak the generated definition to make it OpenAPI-compatible.

Helen
  • 87,344
  • 17
  • 243
  • 314
26

1 - Paste a response in http://www.mocky.io and get a link to your response

2 - Go to https://inspector.swagger.io/ and make a call to your example response

3 - Select the call from "History" and click "Create API definition"

4 - The swagger definition will be available at https://app.swaggerhub.com/

Felipe Cardoso
  • 277
  • 3
  • 4
6

You can use mock-to-openapi cli tool that generates OpenAPI YAML files from JSON mock.

npm install --global mock-to-openapi

then run the conversion of all *.json files from the folder:

mock-to-openapi ./folder/*.json`

Let's have, for example, json object with:

{
    "title": "This is title",
    "author": "Roman Ožana",
    "content" : "This is just an example",
    "date": "2020-05-12T23:50:21.817Z"
}

Tool mock-to-openapi converts JSON to the OpenAPI specification as follows:

type: object
properties:
  title:
    type: string
    example: This is title
  author:
    type: string
    example: Roman Ožana
  content:
    type: string
    example: This is just an example
  date:
    type: string
    format: date-time
    example: 2020-05-12T23:50:21.817Z
OzzyCzech
  • 9,713
  • 3
  • 50
  • 34
0

This works for me:

Generate Swagger REST-client code (and POJO) from sample JSON:

  1. Go to apistudio.io:

    • Insert -> New Model.
    • CutNpaste your JSON.
    • [The Swagger YML file will be generated]
    • Download -> YAML.
  2. Go to editor.swagger.io:

    • CutNpaste the YML saved from last step.
    • Generate Client -> jaxrs-cxf-client (there are many other options).
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Sanjay Das
  • 180
  • 3
  • 14