Network Automation

Python and JSON for the ENCOR Exam: Reading Scripts Under Pressure

Reading a Python automation script and JSON payload for the ENCOR exam
In: Network Automation

The ENCOR exam does not ask you to be a software developer, but it does ask you to read a Python script and a JSON payload and understand what they do. Under exam pressure, a screen of code you half-recognise is a trap; a screen of code whose structure you can parse in ten seconds is free marks. This is a comprehension skill, not a coding skill, and it is entirely learnable.

This article covers the Python and JSON you need to read (not write) automation code for the exam and for real work. It extends the Network Automation cluster guide.

JSON First: It Is Just Nested Data

Every network API you will meet (RESTCONF, Catalyst Center, SD-WAN Manager, Meraki) speaks JSON, so read it fluently. JSON has exactly two container types and a handful of value types, and that is the whole language:

Object { }
Key-value pairs. In Python this becomes a dict. Access a value by its key.
Array [ ]
An ordered list of values. In Python this becomes a list. Access by index, or iterate.

Everything else is values inside those two: strings (in quotes), numbers, booleans (true/false), and null. Reading JSON is just tracing the nesting. Here is a typical API response:

{
  "response": [
    { "hostname": "core-1", "mgmtIp": "10.1.1.1", "reachable": true },
    { "hostname": "core-2", "mgmtIp": "10.1.1.2", "reachable": false }
  ]
}

Trace it: the top level is an object with one key, responsearray of two objects, each with three keys. To get the first device's hostname you walk the path: into response, take element [0], take key hostname. That is data["response"][0]["hostname"] and it is the single most common operation in all network automation.

The Python You Need to Recognise

Map JSON onto Python and most scripts become readable:

dict{"key": "value"} - a JSON object. Access with d["key"].
list[a, b, c] - a JSON array. Access with l[0], iterate with for x in l.
for loopfor device in devices: - do something to each item in a list.
ifif device["reachable"]: - branch on a condition.

Put those together and a real automation snippet reads like plain English:

import requests

resp = requests.get(url, headers=headers, auth=auth, verify=True)
data = resp.json()                      # parse JSON into a Python dict

for device in data["response"]:         # loop the array of devices
    if not device["reachable"]:         # branch on the boolean
        print(f"DOWN: {device['hostname']} ({device['mgmtIp']})")

Read it top to bottom: make an HTTP GET, turn the JSON body into a dict with .json(), loop over the list of devices, and print the ones that are not reachable. If you can narrate a script like that in your own words, you can answer the exam questions, because they ask exactly that: "what does this print?"

The Three Libraries to Recognise

You do not need to memorise their APIs, but you should recognise what each one is for the moment you see its import:

requests
import requests. HTTP: talking to REST and RESTCONF APIs. .get(), .post(), .patch(), .json().
ncclient
from ncclient import manager. NETCONF: manager.connect(), get_config(), edit_config().
json
import json. json.loads() (text to dict), json.dumps() (dict to text).

When you see import requests, the script talks to a REST API. When you see from ncclient import manager, it is NETCONF. When you see import json, it is shuffling JSON to or from text. Recognising the library tells you the script's job before you read a line of logic.

Exam Technique: Reading a Script Under Pressure

A repeatable method for the "what does this code do / output" questions:

  1. Read the imports first. They tell you the domain (REST, NETCONF, JSON) instantly.
  2. Find the data structure. Is the script working on a dict or a list? Locate the JSON it parses and picture its shape.
  3. Trace the access path. data["response"][0]["hostname"] is just "into response, first element, its hostname." Walk it one step at a time.
  4. Follow the loop and the condition. What is it iterating, and what does the if select? The output is whatever survives the filter.
  5. Ignore what you do not need. Error handling, headers, and auth setup are usually not what the question hinges on. Find the line that produces the answer.

The questions are testing comprehension, not recall. You are not asked to write the script; you are asked to say what it does. That is a skill you build by reading a handful of real scripts until the structure is automatic, which is exactly why the rest of this cluster shows real RESTCONF and NETCONF scripts against a live device.

Common Gotchas the Exam Loves

  • Dict access vs list access. data["response"] (a key, for an object) vs data[0] (an index, for an array). Mixing them up is the classic error, and a favourite distractor.
  • JSON true vs Python True. JSON uses lowercase true/false/null; Python uses True/False/None. .json() converts them, but a question may show both forms.
  • Strings vs numbers. "10" (a string) is not 10 (a number). JSON preserves the distinction, and comparisons care.
  • The .json() call. A requests response is not a dict until you call .json() on it. Code that indexes the raw response object is wrong.

FAQ

Do I need to write Python for the exam?

No. You need to read it: given a script, say what it does or outputs. That is comprehension, which is far easier than writing, and entirely learnable by reading real scripts.

How do I access a nested JSON value?

Walk the path one step at a time. data["response"][0]["hostname"] means: into the response key, take the first list element, take its hostname key. Dicts use keys in quotes; lists use numeric indices.

What does .json() do?

It parses the JSON text of an HTTP response into a Python dict (or list). Until you call it, the response is not indexable as data.

How do I tell REST code from NETCONF code?

The import. import requests is REST/RESTCONF; from ncclient import manager is NETCONF. The library announces the domain.

Why does true not work in my Python?

JSON uses lowercase true; Python uses capitalised True. Inside Python code use True; you only see lowercase true in raw JSON text.

Key Takeaways

  • The exam tests reading Python and JSON, not writing them. It is comprehension, and it is learnable.
  • JSON is two containers: object { } (a Python dict, keyed) and array [ ] (a Python list, indexed). Everything else is values inside them.
  • The core operation is walking a nested path: data["response"][0]["hostname"] = into response, first element, its hostname.
  • Recognise three libraries by their import: requests (REST), ncclient (NETCONF), json (parsing).
  • Read a script by: imports first (the domain), then the data shape, then the access path, then the loop/condition.
  • Watch the gotchas: dict-key vs list-index access, JSON true vs Python True, and remembering that a response needs .json() before you can index it.

Next: apply it against a live device in RESTCONF on Cisco IOS XE, or the Network Automation cluster guide.

Written by
More from Ping Labz
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Ping Labz.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.