Plenty of tools want your stack described as JSON: a documentation generator, an inventory script, a dashboard that imports service definitions, a teammate's tooling that only speaks JSON. Your stack, meanwhile, lives in a docker-compose file, because that is what Docker reads. The good news is that a compose file is ordinary YAML, a set of service definitions written in YAML's indentation based structure, so converting it is a format job, not a translation project.

This guide shows how to convert docker-compose YAML settings into JSON with the free YAML to JSON Converter on Ovatly, what the services, ports and environment sections look like on the other side, and how to check the result service by service before anything relies on it.

Contents

  • Quick answer
  • What a compose file looks like as JSON
  • How to convert, step by step
  • Worked example: a two-service stack
  • Anchors and shared settings in compose files
  • Checking the converted structure, service by service
  • Limitations and honest tradeoffs
  • Common mistakes
  • FAQs
  • Conclusion
  • References

Quick answer

Paste your whole compose file into the YAML to JSON Converter and convert. The services mapping becomes a nested JSON object, one object per service; list settings such as ports, volumes and environment stay as JSON arrays; and any anchors you used for shared settings arrive expanded into full values at each service. Then read the output service by service and confirm each service kept the settings it had in the YAML before you hand the JSON to another tool.

What a compose file looks like as JSON

The shape survives the trip almost unchanged, because YAML mappings and JSON objects are the same idea written two ways. The top level of a compose file holds keys such as services, volumes and networks, and each becomes a JSON object with the same name.

Under services, every service name becomes a key whose value is an object of that service's settings: image, build, ports, environment, depends_on and the rest, nested exactly as deeply as the YAML nested them. Settings written as YAML lists stay lists. A port mapping written as - "8080:80" is one string in an array, and an environment entry written as - MODE=production is one string in its array. Settings written as nested mappings, such as a mapping style environment block, become nested objects.

Two shape changes are worth knowing in advance. Values keep the type YAML gave them, so an unquoted number stays a number and a quoted port stays a string, which is one reason compose files quote their port mappings. And anything YAML reads as a date becomes an ISO date-time string, which occasionally surprises people who used a date-like word as a plain label.

How to convert, step by step

  1. Open the YAML to JSON Converter and paste the entire compose file, top level keys and all.
  2. Convert. If the file has a duplicate key or a syntax problem, you get an error rather than output; fix the YAML and convert again.
  3. Look at the top level of the JSON and confirm services is there with every service you run.
  4. Walk through the services one at a time, as described in the checking section below.
  5. Copy or download the JSON, and keep the compose file as the original you continue to edit and deploy from.

Worked example: a two-service stack

Take a small, typical compose file:

services:
  web:
    image: nginx
    ports:
      - "8080:80"
    environment:
      - MODE=production
  db:
    image: postgres
    environment:
      - POSTGRES_PASSWORD=secret

Converted, it becomes:

{
  "services": {
    "web": {
      "image": "nginx",
      "ports": ["8080:80"],
      "environment": ["MODE=production"]
    },
    "db": {
      "image": "postgres",
      "environment": ["POSTGRES_PASSWORD=secret"]
    }
  }
}

Read it against the original and the correspondence is one to one: two services, each with its image, the web service with its port mapping as a one item array of strings, and each environment list carried over untouched. That one to one quality is what you are checking for in a real file: nothing dropped, nothing moved, nothing retyped.

Anchors and shared settings in compose files

Larger compose files often define shared settings once, in an extension block, and merge them into several services with an anchor:

x-common: &common
  restart: "no"
  logging:
    driver: json-file

services:
  web:
    <<: *common
    image: nginx
  worker:
    <<: *common
    image: myapp

JSON has no anchors and no merge key, so the converter expands them. In the output, both web and worker carry restart and logging inline, in full, and the x-common block also appears as its own top level key holding the same values. The JSON is self-contained and arguably easier for another program to consume, since it never has to resolve a reference. Just remember the maintenance consequence: the JSON copies are independent, so if the shared setting changes, reconvert from the compose file rather than editing one service's copy.

Checking the converted structure, service by service

Do not skim the output and trust it. A fast, reliable pass takes two minutes:

  1. Count the services. The JSON should name exactly the services in the compose file, no more, no fewer.
  2. For each service, compare its keys against the YAML: image or build, ports, environment, volumes, depends_on. A missing key usually means an indentation slip in the source, where the setting slid under a neighbouring service or out to the top level.
  3. Spot check one list per service. Ports and environment entries are the settings other tools most often consume, and the ones where a quoting difference shows.
  4. If you used anchors, confirm one expanded service really carries the shared settings, such as restart, inline.

Limitations and honest tradeoffs

The converter converts structure; it does not understand Compose. It will not warn you about a misspelled setting name, a port that clashes, or a service that depends on one that does not exist. Validation of the stack itself remains a job for Docker Compose.

The JSON is also a snapshot. Compose files change as a stack evolves, and the converted file does not follow. Treat the JSON as an export for a specific purpose at a specific moment, and regenerate it when the stack changes. Comments in the compose file, including the notes you left yourself about why a setting exists, are dropped, because JSON has nowhere to put them. And duplicate keys in the YAML are an error that stops conversion, never a silent choice between the two values.

Common mistakes

  • Converting a fragment. Paste the whole file, including the top level services key, or the JSON will not have the shape other tools expect.
  • Trusting the output without the service by service pass. A key that slid one indent level in the YAML converts without complaint and lands in the wrong service.
  • Editing the JSON as the new source of truth. The compose file is what Docker runs. Edit it, then reconvert.
  • Forgetting the anchor expansion. Shared settings exist as independent copies in the JSON; changing one copy changes nothing else.
  • Leaving date-like or yes-like values unquoted. YAML may read them as timestamps or booleans. Quote any value that must remain an exact string.

FAQs

Can I convert the JSON back into a compose file? Yes. The converter works in both directions, so the JSON can be turned back into YAML. Keep in mind that anchors come back as plain repeated values, not as anchors, and comments from the original file are not restored.

What happens to the anchors I use for shared settings? They are expanded. A service that merged an anchored block with the << key receives those settings inline in the JSON, in full, at every service that used them. The values are all there; the single shared definition is not.

Does the tool check that my compose file is valid for Docker? No. It converts the YAML structure into JSON structure. Whether an image name exists, a port is sensible or a service definition is complete is a question for Docker Compose itself, not for a format converter.

Why did one of my values change shape during conversion? Usually because YAML read it as something other than text. Values that look like dates become ISO date-time strings, and unquoted values like yes or numbers are read as booleans or numbers. Quote the value in the compose file if it must stay a plain string.

Is my compose file uploaded anywhere? No. The conversion runs in your browser with js-yaml, on your device. Compose files often contain internal hostnames, ports and environment values, and none of that leaves your machine.

Conclusion

A compose file converts cleanly because it is ordinary YAML: services become nested objects, lists stay lists, and anchored shared settings arrive expanded and self-contained. Convert with the YAML to JSON Converter, then spend two minutes checking the output service by service before another tool builds on it. If YAML conversion surprises in general are your problem, our companion guide on YAML to JSON mistakes: indentation and anchor traps covers the four traps in depth. For more guides in this series, see our Data and JSON Tools guides.

References