Convert a Plain YAML List into JSON for a Test Fixture

Translate a basic list of fictional stations into JSON and verify how the simplified parser represents scalar list items.

By DevSphere Editorial · · 5 min read

Open YAML Tools

Use only the supported subset

A test fixture may need the same small dataset in YAML and JSON forms. YAML Tools has a YAML to JSON tab that parses common indentation-based objects and lists, then serializes the result as JSON. The parser is deliberately simple; it is not a full YAML specification implementation. This example avoids anchors, aliases, multiline scalars, tags, flow collections, and complex keys so the conversion can be inspected with a reasonable expectation. Use invented station labels rather than actual environmental or operational data. The goal is to create a draft fixture, not to prove that arbitrary YAML can be translated losslessly. Keep a copy of the input and consult the parser used by your application when using more advanced syntax. A successful result here is evidence only for the behavior of this subset.

stations:
  - Cedar Demo
  - Birch Demo
active: false

Run the YAML to JSON action

Select YAML to JSON from the row of tabs and confirm the input label says YAML Input. Paste the sample and click Convert to JSON. The parser walks indentation, identifies the stations list, and keeps each plain list item as a string. The output uses the selected Indent Size for JSON serialization. Check that the root object has stations and active, that stations contains two strings, and that active is false. Use Copy or Download if you need to move the result into a test fixture; the download extension is chosen as JSON for this tab. Do not use the generated file as an authoritative schema. The component may not detect every malformed construct or report standards-grade line and column diagnostics. Validate the output with the application's JSON parser before including it in a test.

Understand indentation and list handling

The parser infers a list when a key with no inline value is followed by a line beginning with a dash. Plain scalar items are the safest case: the dash content is passed to the simple scalar reader and appended to the array. A list item beginning with a key-value pair is more limited; subsequent indented fields may not attach to that object as a full YAML parser would. Consistent spaces make parent-child levels easier to interpret. Tabs, unusual indentation widths, and inline arrays with YAML-specific syntax are not safe assumptions. The output order should remain Cedar Demo then Birch Demo. Verify the count and spelling, and do not infer object-property handling from a successful scalar-list example.

{
  "stations": ["Cedar Demo", "Birch Demo"],
  "active": false
}

Review scalar conversions cautiously

The YAML Tools scalar parser recognizes several spellings of true and false, several null spellings, integers, simple decimal values, quoted strings, and inline array or object text when it can parse it as JSON. That is a small convenience subset, not the complete YAML type system. For example, a value that resembles a date, exponent-form number, or YAML-specific boolean may be preserved as text or interpreted differently from another parser. Quoted string handling strips its delimiters but does not implement every escape rule. For exact preservation, inspect the output type and test representative edge cases with the same full parser used by the application. Do not assume YAML-to-JSON preserves comments; comments are skipped and are not represented in JSON. If the meaning of a comment is important, carry it into a separate note or keep the original file.

Troubleshoot and verify the fixture

If the conversion returns an incomplete structure, compare the input's indentation and list item markers, and remove syntax outside the supported subset before concluding that the source itself is invalid YAML. An empty input produces an error; a successful output can still be an inaccurate parse if the document uses a construct the lightweight parser does not understand. Verify that stations contains two strings in order and that active is false. Reparse the generated JSON in the actual test environment. If the application requires official YAML 1.2 behavior, use that library or a schema-aware pipeline rather than the browser approximation. Clear the workspace before another file, and keep production values out of the sample. This workflow is well suited to a tiny fixture with ordinary keys and scalars, not deployment validation or migration of arbitrary YAML repositories.

Check the generated JSON as a consumer would: stations remains an array, both names remain strings, and active remains a boolean. JSON alone does not preserve YAML comments or source formatting. For a fixture, add a separate assertion for the expected array length and each item in order. If you change the YAML sample to include object-valued list items or empty structures, test those cases independently rather than assuming this simple parser composes correctly at any depth. The Download button saves output.json for this mode, while Copy writes the visible result to the clipboard. Review the file after saving; a copied output can be truncated or pasted into the wrong location. Keep YAML and JSON examples together only when their relationship is clear to maintainers.

For this scalar-list case, a useful acceptance check is deliberately small: the top-level value must be an object, stations must be a two-element array, and active must be boolean false. Re-run that check after changing Indent Size or copying output, since whitespace settings should not change parsed values. If an automated fixture test disagrees with the preview, trust the production parser and capture the minimal failing YAML as a regression case. That keeps the example focused on behavior the lightweight implementation actually supports.

Browse all tutorials