Skip to content
Study CCNP

6.2 Construct valid JSON-encoded files

4 min read ENCOR 350-401 v1.2 Updated

Aligned to Cisco's 350-401 ENCOR v1.2 exam topics.

On this page

What this objective tests

This is a construct objective. The exam shows you JSON text. You must build valid JSON or find the syntax error.

JSON is the payload format of most network APIs. Catalyst Center, SD-WAN Manager, and RESTCONF all use it. A script fails on malformed JSON before it touches the network.

The six building blocks

A JSON document uses six types of values:

TypeExampleRule
Object{"hostname": "R1"}Key-value pairs in curly braces. Keys are strings.
Array["R1", "R2"]Ordered items in square brackets.
String"IOS-XE"Always double quotes. Never single quotes.
Number24 or 99.5No quotes.
BooleantrueLowercase only.
NullnullLowercase only. Means no value.

Object rules:

  • Put every key in double quotes.
  • Put a colon after every key.
  • Separate pairs with commas.
  • Do not put a comma after the last pair.

Nesting is allowed. An object can hold an array. An array can hold objects. Most API payloads are nested.

Common errors

This table covers the errors the exam uses most:

ErrorInvalidValidWhy it fails
Trailing comma{"a": 1,}{"a": 1}JSON forbids a comma before the closing brace.
Single quotes{'a': 1}{"a": 1}JSON requires double quotes.
Unquoted key{a: 1}{"a": 1}Keys must be strings.
Comments{"a": 1} // note{"a": 1}JSON has no comment syntax.
Python booleans{"ok": True}{"ok": true}JSON booleans are lowercase.
Python null{"site": None}{"site": null}JSON uses null, not None.

Exam habit: check punctuation before meaning. A payload can describe the correct network intent and still be invalid JSON.

Example: a valid device inventory

This file is complete and valid. Use it as a model:

{
  "site": "Branch-10",
  "devices": [
    {
      "hostname": "BR10-R1",
      "mgmt_ip": "10.10.10.1",
      "platform": "IOS-XE",
      "restconf_enabled": true,
      "interfaces": ["GigabitEthernet1", "GigabitEthernet2"],
      "snmp_contact": null
    },
    {
      "hostname": "BR10-SW1",
      "mgmt_ip": "10.10.10.2",
      "platform": "IOS-XE",
      "restconf_enabled": false,
      "interfaces": ["GigabitEthernet1/0/1"],
      "snmp_contact": "[email protected]"
    }
  ]
}

Read the structure:

  • The top level is an object with two keys: site and devices.
  • devices holds an array of two objects.
  • interfaces holds an array of strings.
  • snmp_contact shows both null and a string. Both are valid.

Validate any file from the command line:

python3 -m json.tool inventory.json

Valid input prints formatted JSON. Invalid input prints an error with a line and column number.

Lab: fix the broken JSON

This lab gives you a broken file. Fix it. Then compare with the corrected version.

Start with this file named broken.json:

{
  'site': 'HQ',
  "devices": [
    {
      "hostname": "HQ-R1",
      "mgmt_ip": "10.10.10.11",
      "restconf_enabled": True,
    },
    {
      "hostname": "HQ-SW1",   // access switch
      "mgmt_ip": "10.10.10.12"
    }
  ],
}

Do these steps:

  1. Run python3 -m json.tool broken.json.
  2. Read the first error. Fix that line.
  3. Run the command again. Fix the next error.
  4. Repeat until the command prints formatted JSON.
  5. Compare your file with the fixed version below.

Fixed version:

{
  "site": "HQ",
  "devices": [
    {
      "hostname": "HQ-R1",
      "mgmt_ip": "10.10.10.11",
      "restconf_enabled": true
    },
    {
      "hostname": "HQ-SW1",
      "mgmt_ip": "10.10.10.12"
    }
  ]
}

The broken file had five errors:

  1. Line 2: single quotes around 'site' and 'HQ'. Changed to double quotes.
  2. Line 7: True is a Python boolean. Changed to lowercase true.
  3. Line 7: trailing comma after the last pair in the object. Removed the comma.
  4. Line 10: a // comment. JSON forbids comments. Removed the comment.
  5. Line 13: trailing comma after the last element of the devices array. Removed the comma.

Note: Fix errors one at a time. The parser stops at the first error. Re-run the validator after every fix.

Exam traps

  • JSON strings and keys need double quotes. Single quotes fail.
  • JSON forbids trailing commas and comments. JavaScript allows them; JSON does not.
  • true, false, and null are lowercase. True, False, and None are Python.
  • Numbers have no quotes. "24" is a string, not a number.
  • A missing comma between pairs is as invalid as an extra comma.

Pass check

You are ready when you can do these things:

  • Build a nested device inventory from scratch without a syntax error.
  • Find a trailing comma, a single quote, or a comment by eye.
  • Explain the difference between JSON null and Python None.
  • Validate a file with python3 -m json.tool and read the error location.

Sources used

  • Cisco ENCOR 350-401 v1.2 exam topics: https://learningcontent.cisco.com/documents/marketing/exam-topics/350-401-ENCORE-v1.2.pdf

Related objectives