Data Serialization

Structuring data in machine readable formats

We need structured data formats to make data machine readable. If these are also human readable it can make human interaction with this structured data easier and more efficient.

Common structured data formats are
XML = eXtensible Markup Language (Covered in last section)
JSON = JavaScript Object Notation
YAML = YAML Ain’t Markup Language (recursive acronym, nice)

The API you’re using disctates the required structured data formats you will be using.

Common Data Types

Strings: Sequences of alphanumeric characters interpreted as text
Numbers: (Different types of numbers: Integer, double, float.etc) Interpreted as a number that arithmetic can be performed against.
Boolean: True or False
Null: An empty value type
List / Sequence / Array: A set of values or data types
Dictionary /object(json dict)/ mapping(yaml dict): Set of Key:Value pairs

JSON – JavaScript Object Notation

JSON is case sensitive.
JSON ignores whitespace / indentation.
No way to add comments to JSON code.
JSON uses braces {for objects} and brackets [for arrays] for data structures.
Uses Objects (like a dict in python) and Arrays (basically a list in python).

JSON Strings
‘Escape’ character is backslash \ (same as in python, so you can tell the interpreter that the next character is still part of the string even if it’s a quotation mark(

JSON Numbers
Numbers are represented either in standard decimal or scientific notation
e.g.
1023
0.112
-1.34
1.1e5 = 1.1 * 10^5 = 110000

JSON Object
Set of key : value pairs
Notated with braces {}
Can have nested objects and arrays

{
"hostname":"R1",
"interfaces" : [{
    "name":"ge-0/0/0",
    "address":"10.0.0.1/24"},
    {"name":"ge-0/0/1",
    "address":"10.0.1.1/24"}]
}

JSON Arrays
Ordered sets of values
Notated with brackets []
Can have nested objects and arrays

Mapping output to json in the cli is only available in Junos OS 14.2 and later

example json output – show configuration interfaces | display json

YAML – YAML Ain’t Markup Language

Used by Ansible for playbook creation, also used by PyEz, can use YAML to define or load information for Jinja – YAML is especially human readable and easy to use (I use it a lot at work).

YAML is case sensitive
YAML uses indentation for data structure (spaces, not tabs)
YAML documents start with 3 hyphens (—)
YAML allows for comments by starting with a hash (#) Pythonic!
Strings do not need quotation marks unless they contain special characters like hashes
YAML data structures: Mapping (like a dict) Sequence (like a list)
YAML is a superset of JSON: All JSON is YAML, not all YAML is JSON

Mapping is exactly like it is in Python
Sequences are notated with a hyphen

---

#This is mapping
Key1 : Data1
Key2 : Data2

#This is a Sequence
- 10.0.0.1/24
- 10.0.1.1/24
- 10.0.2.1/24

You can also have nested mappings and sequences

---
host-name : R1
version : 17.3R1.10
authentication-servers :
  - 10.0.0.1
  - 10.0.0.2
interfaces :
  - name : ge-0/0/0
    address :
      - name : 10.0.1.1/24
        primary : true
      - name : 10.0.2.1/24
        primary : false