Networking Data & Formats
Choosing the Right Format for the Job
What you'll learn
- A practical decision framework for choosing between JSON, YAML, and CSV
- How to read data from two different files and combine it into one result
- How everything from this module — and the Python module before it — comes together in one real script
- That the choice of format is a design decision, not a fixed rule
Listen to a guided explanation of this lesson.
Spoken narration isn't supported in this browser.
The decision, made concrete
Three lessons, three formats — here's the question that actually decides between them:
- Is the data a flat table — the same columns, every row?
- Yes → CSV
- Is it primarily something a person will hand-edit directly?
- Yes → YAML
- Otherwise → JSONnested data, exchanged between programs
A device inventory that's just hostname/IP/platform, one row each? CSV. An Ansible-style playbook or Nornir inventory a human will open and tweak by hand? YAML. A device's full config pulled from a REST API, with nested sections for interfaces, VLANs, and routing? JSON.
Bringing it together: merging two files into one report
Real automation work often means combining data from more than one source. Here's a small, complete script that reads a CSV device inventory and a JSON site-configuration file, and merges them into a single combined report — using nothing beyond what this module and the Python module have already covered.
The inputs:
hostname,ip,site
SW1,10.10.10.1,HQ-Fl2
SW2,10.10.10.2,HQ-DC
SW3,10.10.10.3,HQ-Fl2A flat device list — exactly the shape from the last lesson.
{
"HQ-Fl2": "10.0.0.5",
"HQ-DC": "10.0.0.1"
}A small nested lookup — which NTP server each site expects. Note this genuinely doesn't fit CSV's flat-row shape well, which is exactly why it's JSON instead.
The script:
import csv
import json
with open("devices.csv") as f:
devices = list(csv.DictReader(f))
with open("site_config.json") as f:
site_config = json.load(f)
combined = []
for device in devices:
site = device["site"]
expected_ntp = site_config.get(site, "unknown")
combined.append({
"hostname": device["hostname"],
"ip": device["ip"],
"site": site,
"expected_ntp": expected_ntp,
})
with open("combined_inventory.json", "w") as f:
json.dump(combined, f, indent=4)
print(f"Merged {len(combined)} devices into combined_inventory.json")Save all three files in the same folder and run 'python merge_inventory.py' — no libraries beyond the standard library, no real devices.
What it does, tying back to what you already know:
csv.DictReaderreads the flat inventory into a list of dictionaries — Lesson 4.json.loadreads the nested site config into a Python dictionary — Lesson 2.for device in devices:— the loop from "Conditions & Loops," visiting every device once.site_config.get(site, "unknown")looks up each device's site in the config dictionary, with"unknown"as a safe fallback if a site isn't listed — so a missing site doesn't crash the script, it just gets flagged.combined.append({...})builds a new dictionary per device, joining fields from both sources — the same result-collection pattern from the Python module's capstone.json.dump(..., indent=4)writes the merged result out as a new, readable JSON file — Lesson 2 again, now producing output instead of just reading input.
Running it prints:
Merged 3 devices into combined_inventory.jsonOne line, confirming what happened and where to find the result — the same honest, minimal reporting habit this curriculum has kept since 'Network Automation Fundamentals.'
And it creates:
[
{
"hostname": "SW1",
"ip": "10.10.10.1",
"site": "HQ-Fl2",
"expected_ntp": "10.0.0.5"
},
{
"hostname": "SW2",
"ip": "10.10.10.2",
"site": "HQ-DC",
"expected_ntp": "10.0.0.1"
},
{
"hostname": "SW3",
"ip": "10.10.10.3",
"site": "HQ-Fl2",
"expected_ntp": "10.0.0.5"
}
]Every device now carries its expected NTP server, looked up by site — data from two different files and two different formats, combined into one. This is exactly the kind of file a later script (once Netmiko is in the picture) could read back in to actually check each device against.
The choice is a design decision, not a rule
There's no single correct answer carved in stone — a small, flat inventory could reasonably live in JSON too, and plenty of real tools accept more than one format for the same kind of input. What matters is being able to reason about why a given format fits a given job, which is exactly what this module built toward: JSON for nested data moving between programs, YAML for what a person will hand-edit, CSV for flat tables — and, as this lesson showed, combining more than one when a real task calls for it.
Where this module leaves you
You can now read and write the three formats that carry almost all real network automation data — JSON for nested data exchanged with programs and APIs, YAML for configuration a person hand-edits, and CSV for flat inventory lists — and, just as importantly, reason about which one fits a given job. Combined with the Python module before it, you have everything needed to read a real inventory, process it, and produce a real report, in the actual formats network automation tooling uses.
What's ahead
The next module puts this together into a complete, end-to-end automation project — still without needing a real device, before "Netmiko Fundamentals" connects everything you've learned to actual SSH sessions on real switches and routers.
Key takeaways
- A simple decision framework: flat table, same columns every row → CSV. Something a person hand-edits directly → YAML. Nested data exchanged between programs → JSON.
- Real automation tasks often combine data from more than one source and format, not just one file in one format.
- dict.get(key, default) looks up a value safely, with a fallback instead of crashing when a key is missing.
- This module's three formats plus the Python module's variables, loops, functions, and exception handling are now enough to build a real, working data pipeline.
- Choosing a format is a design decision based on the shape of the data and who needs to read it — not a fixed, one-size-fits-all rule.