0

I am trying to configure swagger for my application. Being new to this field I went to different tutorials and tried to convert the below json to YAML but it's giving errors like bad indentation, response missing etc. The main problem I am facing is in recognizing syntax to represent array of list in YAML format, then to add block in YAML which shows expected values for a particular block.

JSON Format to be converted to YAML:

  {
       "abc":[
          {
             "xyz":[ //array of list
                {
                   "id":"",
                   "name":"",
                   "relation":[ //array of list
                      {
                         "first":{
                            "xxx":"",
                            "xxx":"",
                            "xxx":[ //array of string
                               ""
                            ]
                         },
                         "second":{
                            "xxx":"",
                            "xxx":"",
                            "xxx":[
                               ""
                            ],
                            "type":""
                         }
                      },
                      {
                         "first":{
                            "xxx":"",
                            "xxx":"",
                            "xxx":[ //array of string
                               ""
                            ]
                         },
                         "second":{
                            "xxx":"",
                            "xxx":"",
                            "xxx":[
                               ""
                            ],
                            "type":""
                   }
                }
             ],
             "rows":[

             ]
          }
       ]
    }

YAML is as below:

    swagger: "2.0"
    info:
      version: 1.0.0
      title: xxxx
      description: xxxx
    schemes:
      - https
    host: xxxx
    basePath: xxxx
    paths:
      /xxx:
        post:
         summary: xxxx
         consumes:
            - application/json
         produces:
            - application/json
         parameters:
    abc:
        - xyz:
            id: string
            name: string
            relation: string
        - first:
            id: string
            name: string
            relation: string
          second:
             id: string
            name: string
            relation: string
        - first:
            id: string
            name: string
            relation: string
          second:
            id: string
            name: string
            relation: string

    responses:
            '200':
              description: Created
Rishi
  • 51
  • 6
  • Your YAML is not valid Swagger. Paste it into http://editor.swagger.io and fix the errors. See if this helps: https://swagger.io/docs/specification/2-0/basic-structure/ – Helen Aug 29 '17 at 10:09
  • Related: [Swagger array of objects](https://stackoverflow.com/q/43711744/113116), [Array of objects as an input parameter in swagger](https://stackoverflow.com/q/39627929/113116), [How to describe a model in Swagger for an array with simple objects?](https://stackoverflow.com/q/19585581/113116), [How to define array-of-objects as parameter?](https://stackoverflow.com/q/40640243/113116) – Helen Aug 29 '17 at 10:12

1 Answers1

0

When working with YAML you talk about sequences (besides map an scalar). A sequences is what gets mapped to a list in Python and an array in some other languages.

So if you are talking about "represent array of list in YAML" you are actually referring to a sequence of sequences. There are three ways to represent this in YAML.

block-style within block-style:

- - a
  - b
- - c
  - d

, flow-style within block-style:

- [a, b]
- [c, d]

, or flow-style within flow-style:

[[a, b,], [c, d,],]

Any online YAML parser will show you that the above amounts to the same.

Please note:

  • You cannot have block-style within flow-style
  • You can have trailing commas (something that JSON doesn't allow, and which makes JSON unnecessarily hard to edit for humans).

In your example YAML output, which is correct YAML, there are no sequence of sequences (or array of lists in your terminology).

Anthon
  • 69,918
  • 32
  • 186
  • 246