NetAuto Academy

Networking Data & Formats

Choosing the Right Format for the Job

Beginner14 min read

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
🎓 Teach This Lesson

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:

  1. Is the data a flat table — the same columns, every row?
  2. Yes → CSV
  3. Is it primarily something a person will hand-edit directly?
  4. Yes → YAML
  5. 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.

Quick checkScenario
A team keeps a flat list of 200 switches — hostname, IP, site, platform, one row per switch, same columns every time. Which format fits best?
Quick checkScenario
A team is writing an Ansible-style playbook that engineers will open in a text editor and hand-edit regularly. Which format fits best?

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:

devices.csv
hostname,ip,site
SW1,10.10.10.1,HQ-Fl2
SW2,10.10.10.2,HQ-DC
SW3,10.10.10.3,HQ-Fl2

A flat device list — exactly the shape from the last lesson.

site_config.json
{
  "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:

merge_inventory.py
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.DictReader reads the flat inventory into a list of dictionaries — Lesson 4.
  • json.load reads 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:

terminal
Merged 3 devices into combined_inventory.json

One 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:

combined_inventory.json
[
  {
      "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.

Quick checkIdentify the correct code
Which line in the script safely looks up a device's site in site_config, without crashing if that site happens to be missing?
Quick checkConcept check
Why does this script read from two different files instead of putting everything in one?

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.

Quick checkMyth check
Is there one 'correct' format that should always be used for network automation data?

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.