YAML vs JSON: Which Actually Parses Faster

Updated

yaml vs json

JSON parses much faster than YAML. In the run shown below, on the one machine tested (Python 3.9.6, PyYAML 6.0.2), PyYAML’s own safe_load took 121x longer than json.loads for the same 10,000 round trips, and even the C-accelerated YAML loader was 18x slower.

Use JSON when parse speed matters (APIs, hot paths, high request volume); use YAML when a human is going to hand-edit the file (Kubernetes manifests, CI config, Docker Compose) and speed isn’t the bottleneck.

The benchmark: 10,000 JSON and YAML round trips

Here is the benchmark, actually executed:

python: 3.9.6 macOS-26.6-arm64-arm-64bit
PyYAML: 6.0.2
CSafeLoader available: True
JSON  (json.dumps/json.loads)      x10000: 0.02641 sec
YAML  (yaml.dump/yaml.safe_load)   x10000: 3.21010 sec
ratio yaml_time / json_time: 121.54x
YAML  (CSafeDumper/CSafeLoader)    x10000: 0.47301 sec
ratio cyaml_time / json_time: 17.91x

Same payload both times: {"name": "John Doe", "age": 30, "skills": ["Python", "DevOps"]}, dumped and immediately parsed back, 10,000 times, timed with time.perf_counter(). The first YAML run uses yaml.safe_load as most code actually calls it, which is pure-Python parsing. PyYAML also ships C bindings against libyaml (CSafeLoader/CSafeDumper) when available, which narrows the gap to 18x in that run but does not close it. The multiples change from run to run on the same machine, so read them as the size of the gap in one run, not as constants.

Why the gap is real, not a Python quirk

JSON’s grammar is deliberately minimal: objects, arrays, strings, numbers, three literals. A JSON parser has one job. YAML 1.1’s grammar adds anchors and aliases, multiple document markers, block and flow styles, and a type-resolution pass that has to check every scalar against a table of implicit types (is on a string or a boolean? is 22:22 a string or a sexagesimal integer?) before deciding what it is. That extra work happens on every parse, in every language’s YAML library, which is why the direction of this result (YAML slower, not faster) generalizes even though the exact multiple (121x here) is specific to this machine, this payload size, and pure-Python PyYAML.

This is also not a reason to avoid YAML for configuration. A CI pipeline definition gets parsed once per run; the 3-second difference here is for 10,000 round trips in a loop. Parse speed only matters where you’re doing it at volume (request handlers, log ingestion, high-frequency data exchange), and that’s exactly where JSON already wins on other grounds too. PostgreSQL draws a similar line inside JSON itself: jsonb trades the original text for containment, key lookups, and GIN indexing, the same speed-over-fidelity trade this page just measured for YAML versus JSON.

The other real risk, and how to avoid it

YAML’s full loader can construct arbitrary Python objects from tags like !!python/object/apply, which is a code-execution path, not a performance one. On the same test machine, yaml.safe_load() refuses that tag outright:

safe_load raised ConstructorError - blocked: could not determine a constructor for the tag 'tag:yaml.org,2002:python/object/apply:os.system'

Always call yaml.safe_load(), never bare yaml.load(), when the YAML source isn’t fully trusted. JSON has no equivalent construction step to exploit: json.loads() only ever produces dicts, lists, strings, numbers, booleans, and None. yaml-vs-json.sh times 10,000 json and PyYAML round trips and passes if YAML comes out slower, then checks that safe_load rejects the python/object/apply tag; your ratio will differ with your CPU and PyYAML build, and the script needs python3 with PyYAML installed.

For the specific ways YAML’s type resolution silently changes a value’s type depending on which parser reads it, including a plain 22:22 timestamp parsing to two different integers in two different libraries, see the YAML syntax breakdown, which has the measured evidence for that; it isn’t repeated here.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.