Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the practical different between the usage of JSON and YAML in Swagger?

Tags:

json

yaml

swagger

It appears that JSON includes the path information and http request verb, whereas YAML seems to just definition a tree structure alone.

What is the difference between them? Or am I mixing up different concepts/hierarchies here? newbie to swagger, just started learning.

If YAML is a superset of JSON what specifically is the superset adding here - is it URL paths and HTTP verbs ? Is adding an example also something that YAML adds to JSON for Swagger ?

like image 617
Michael Durrant Avatar asked Oct 28 '17 14:10

Michael Durrant


People also ask

What is an advantage of JSON compared to usage of YAML?

YAML, depending on how you use it, can be more readable than JSON. JSON is often faster and is probably still interoperable with more systems. It's possible to write a "good enough" JSON parser very quickly. Duplicate keys, which are potentially valid JSON, are definitely invalid YAML.

What is the difference between YAML and JSON?

The design goal of JSON is to be as simple as possible and be universally usable. This has reduced the readability of the data, to some extent. In contrast, the design goal of YAML is to provide a good human-readable format and provide support for serializing arbitrary native data structures.

What is the difference between JSON and Swagger?

JSON Server belongs to "API Tools" category of the tech stack, while Swagger UI can be primarily classified under "Documentation as a Service & Tools". "Stupid simple" is the top reason why over 2 developers like JSON Server, while over 33 developers mention "Open Source" as the leading cause for choosing Swagger UI.

Is Swagger a JSON or YAML?

Swagger definitions can be written in JSON or YAML. In this guide, we only use YAML examples, but JSON works equally well. A sample Swagger specification written in YAML looks like: swagger: "2.0"


1 Answers

According to the OpenAPI Specification,

An OpenAPI document that conforms to the OpenAPI Specification is itself a JSON object, which may be represented either in JSON or YAML format.

So feature-wise, there is no difference between using JSON or YAML. What YAML as a superset of JSON adds here is merely a different syntax. Using an example from the specification, these two documents are identical:

{
  "servers": [
    {
      "url": "https://development.gigantic-server.com/v1",
      "description": "Development server"
    },
    {
      "url": "https://staging.gigantic-server.com/v1",
      "description": "Staging server"
    },
    {
      "url": "https://api.gigantic-server.com/v1",
      "description": "Production server"
    }
  ]
}

and

servers:
- url: https://development.gigantic-server.com/v1
  description: Development server
- url: https://staging.gigantic-server.com/v1
  description: Staging server
- url: https://api.gigantic-server.com/v1
  description: Production server

The first document is valid JSON and valid YAML (since YAML is a superset of JSON). The second document is valid YAML and structurally identical to the first document.

So to answer the question about what the YAML superset adds here: It adds different syntax for specifying the same structures. Nothing more.

YAML does include some features that are not mappable to JSON, but they are irrelevant here because Swagger/OpenAPI doesn't use them.

like image 51
flyx Avatar answered Oct 26 '22 03:10

flyx