0

Desired Yaml:

path:
  "/":
      get:
        summary: Root
        operationId: root__get

I have a python dictionary like so:

path_dict = {
    path:{
        "/": {
            get: {
                summary: Root
                operationId: root__get
            }
         }
    }
}

When I do yaml.dump(path_dict), I get this (the double quotes disappear):

path:
  /:
      get:
        summary: Root
        operationId: root__get

But when I wrap my path dict "/" in another set of quotes, or using \" like so:

path_dict = {
    path:{
        '"/"': {
            get: {
                summary: Root
                operationId: root__get
            }
         }
    }
}

My yaml file becomes:

path:
  '"/"':
      get:
        summary: Root
        operationId: root__get

Is there a neat way to preserve the " just for the "/" key but not for the rest of the keys? I need my yaml file to be in this specific format to satisfy Swagger2.0 specs.

Shengy90
  • 43
  • 6
  • 1
    YAML just **represents** the data you enter. In the first example your key in Python doesn't contain double quotes, so the YAML representation doesn't contain quotes too. In the second example your key is exactly `"/"`, so the YAML represents exactly these characters (outer quotes needs for preserve meaning of double quotes as data). I am not familiar with swagger, but I expect it to fully support YAML 1.1 or newer spec. – Tsyvarev Jul 30 '22 at 22:12
  • Possibly related: [PyYAML dump format](https://stackoverflow.com/q/20805418/113116) – Helen Aug 02 '22 at 09:20

0 Answers0