Skip to main content

Python utility created to convert JSON to Swagger definitions structure, based on entities concept.

Project description

"Json to Swagger" conversor

Python utility created to convert JSON to Swagger definitions structure, based on entities concept.

Dependencies

  • Python 3.6+
  • Libraries:
    • swagger-parser
    • pyyaml
    • pattern

Note: Before run, execute:

$ pip install -r requirements.txt

Usage

Running:

$ python main.py <json-file-path> <RootEntityName> [swagger-file-path] [-v]
  • json-file-path - path of json file to convert
  • RootEntityName - name of root swagger definitions entity
  • swagger-file-path - path of swagger file. If the file has content, it will be merged with json converted content
  • -v - verbose mode

example.json

{
   "id": "ffe74d36-4f76-4ca6-9dd1-8d5c9b16b056",
   "name": "JSON example",
   "description": "Just a simple JSON file to convert into swagger definition",
   "someObject": {
      "someInteger": 0,
      "someNumber": 0.1,
      "someBoolean": true,
      "someNull": null,
      "someString": "Example string content"
   },
   "items": [
      {
         "name": "Item",
         "subItem": {
            "subItemsOfSubItem": [
               {
                  "foo": "bar"
               }
            ]
         }
      }
   ]
}

example.swagger.yaml

definitions:
  Root:
    type: object
    title: Root
    description: Root Entity
    properties:
      id:
        type: string
        example: ffe74d36-4f76-4ca6-9dd1-8d5c9b16b056
      name:
        type: string
        example: JSON example
      description:
        type: string
        example: Just a simple JSON file to convert into swagger definition
      someObject:
        $ref: '#/definitions/SomeObject'
      items:
        type: array
        items:
          $ref: '#/definitions/Item'
  SomeObject:
    type: object
    title: Some Object
    description: Some Object Entity
    properties:
      someInteger:
        type: integer
        example: 0
      someNumber:
        type: number
        example: 0.1
      someBoolean:
        type: boolean
        example: true
      someNull:
        type: string
        example: null
      someString:
        type: string
        example: Example string content
  Item:
    type: object
    title: Item
    description: Item Entity
    properties:
      name:
        type: string
        example: Item
      subItem:
        $ref: '#/definitions/SubItem'
  SubItem:
    type: object
    title: Sub Item
    description: Sub Item Entity
    properties:
      subItemsOfSubItem:
        type: array
        items:
          $ref: '#/definitions/SubItemsOfSubItem'
  SubItemsOfSubItem:
    type: object
    title: Sub Items Of Sub Item
    description: Sub Items Of Sub Item Entity
    properties:
      foo:
        type: string
        example: bar

Attention: Properties with same name will be merged

Example

In the JSON structure bellow, it has the "items" property in two objects:

{
   "items": [
      {
         "name": "Item",
         "subItem": {
            "items": [
               {
                  "foo": "bar"
               }
            ]
         }
      }
   ]
}

The result will merge the two "items" properties into "Item" entity:

definitions:
  RootEntity:
    type: object
    title: Root
    description: Root Entity
    properties:
      items:
        type: array
        items:
          $ref: '#/definitions/Item'
  Item:
    type: object
    title: Item
    description: Item Entity
    properties:
      name:
        type: string
        example: Item
      subItem:
        $ref: '#/definitions/SubItem'
      foo:
        type: string
        example: bar
  SubItem:
    type: object
    title: Sub Item
    description: Sub Item Entity
    properties:
      items:
        type: array
        items:
          $ref: '#/definitions/Item'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json-to-swagger-0.0.3.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

json_to_swagger-0.0.3-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file json-to-swagger-0.0.3.tar.gz.

File metadata

  • Download URL: json-to-swagger-0.0.3.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.2

File hashes

Hashes for json-to-swagger-0.0.3.tar.gz
Algorithm Hash digest
SHA256 b77f3ab7683155f02c9df659ac9ca2a896f6789e85174712042eab4875ec405c
MD5 64cc8e7530d9ace68140307e6b7c7535
BLAKE2b-256 84b17ec625c16998d1bbfb938f64c820eb2f25b934cf24cac2fb2d7b7b2ae827

See more details on using hashes here.

File details

Details for the file json_to_swagger-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: json_to_swagger-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.2

File hashes

Hashes for json_to_swagger-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ffd038f2cbfe9399cf9e3bc510c00c06ef7c50fc8983649a6433e3e906a82519
MD5 bda8760ba2664fcec91f39855b8d177a
BLAKE2b-256 67c1b210c24d1c14f47f6cfc6ab393a3e23e2556b583fe242d4896a6a830f6ba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page