JSON to YAML With Jinja2 Templates — When Each Path Wins
- Jinja2 + JSON → YAML is common in Ansible playbooks that ingest API data and produce config files.
- Ansible's to_nice_yaml filter does the conversion inline; our browser tool is for prototyping the output shape before writing the template.
- Watch for Jinja2 string escaping inside YAML — template tags can conflict with YAML's own syntax.
Table of Contents
Jinja2 is Ansible's template engine and shows up anywhere Python generates config files. A common pattern: ingest JSON (from an API or config service), pipe through Jinja2 with a to_nice_yaml filter, emit YAML for downstream tools. Our browser JSON-to-YAML converter helps prototype the output shape before writing the template. This guide covers the combined workflow.
The Ansible to_nice_yaml Pattern
In Ansible, to_nice_yaml converts a variable (usually a dict loaded from JSON) to YAML:
- name: Write config
copy:
content: "{{ my_json_data | from_json | to_nice_yaml }}"
dest: /etc/app/config.yaml
This is runtime conversion inside a playbook. For design time — knowing what the output YAML will look like — our browser tool gives you the shape without running the playbook.
Prototyping With the Browser Tool
- Grab a sample JSON from your API or config source.
- Paste into the browser converter.
- See the YAML shape.
- Compare with what your downstream consumer expects.
- Now write the Jinja2 template with
to_nice_yaml, knowing the shape is right.
Ansible's YAML output with to_nice_yaml matches our browser tool closely — same 2-space indent, same quoting rules. Occasional differences around alphabetical key sorting, but the overall shape is predictable.
Jinja2 Tags Inside YAML — Escape Carefully
Jinja2's {{ var }} and {% block %} syntax can conflict with YAML if your JSON contains values that look like tags. Rare, but a gotcha:
# JSON value: "{{ user.name }}"
# Our converter outputs:
greeting: "{{ user.name }}"
Downstream Jinja2 will try to evaluate user.name. If that's not what you want, escape by wrapping in {% raw %}...{% endraw %} blocks or use Jinja2's | escape filter.
Conversely, if you want Jinja2 to evaluate at template render time, pass through — our converter doesn't touch the syntax, Ansible/Jinja will process it later.
Salt, Custom Pipelines, and Python Scripts
Jinja2 ships with Salt (pillar and state files) and is embedded in many Python tools. The JSON-to-YAML conversion pattern applies beyond Ansible:
- Salt:
{{ salt.pillar.get('key') | json_decode | yaml }}in an SLS file. - Custom Python + Jinja2:
yaml.safe_dump(json.loads(json_str))in a helper function, then render through a Jinja2 template. - Helm: uses Go templates, but similar pattern — parse JSON, render YAML output.
The browser tool preview works for any of these. Paste sample input, see the output shape, then wire the template.
When to Skip Jinja2 and Use the Browser Tool
Jinja2's value is templating — variable substitution, loops, conditionals. If your workflow is pure format conversion (JSON in, YAML out, no substitution), skip Jinja2:
- One-off conversion: browser tool.
- Batch conversion with no substitution:
yq -o yaml. - Conversion + variable substitution: Jinja2 is the right tool.
- Conversion + loops/conditionals: Jinja2 or Python with PyYAML.
Pick by what you actually need. Jinja2 for templating, browser for format flips.
Prototype Your Template Output
Paste sample JSON, see the YAML shape, then write your Jinja2 template once.
Open Free JSON to YAML ConverterFrequently Asked Questions
Does Ansible's to_nice_yaml output match this browser tool's output?
Very close. Both emit 2-space indent, quote ambiguous strings, and preserve structure. Minor differences: to_nice_yaml defaults to alphabetical key sorting, our converter preserves source JSON order.
Can Jinja2 consume JSON directly?
Yes with Ansible's from_json filter, or in plain Python with json.loads before rendering. Jinja2 itself is template language-agnostic — it operates on Python dicts/objects.
How do I escape Jinja2 syntax that appears in JSON values?
Wrap in {% raw %}...{% endraw %} blocks, or use Jinja2's |escape filter when rendering. Our browser tool passes tag syntax through unchanged — Jinja2 processes it at render time.
What's the Jinja2 equivalent of converting a list of dicts to YAML?
Just pipe through to_nice_yaml — it handles any nested structure. Or use Python: yaml.safe_dump(list_of_dicts, default_flow_style=False).

