• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Rowell Dionicio

Get Techie With It

  • Home
  • About
  • Resources
    • Archives
    • Book List
    • YouTube
  • Learn
    • DevNet Associate
    • PCNSA Certified
  • Blog
  • Contact
  • Show Search
Hide Search

200-901

Parsing JSON with Python

October 28, 2019 By Rowell 3 Comments

JSON seems to be a popular data structure to use with APIs. I work with Meraki often and noticed the decision they went with. You can see the output easily with an API dev environment such as Postman.

When working with the Meraki API, I receive data in the form of JSON. In my example of parsing JSON data into Python data structure, I’ll be querying the Meraki API dashboard to list all the organizations I have access to. This example leverages the Meraki Sandbox.

Using Postman to test my request, I copy the code to create Python script.

import requests

url = "https://dashboard.meraki.com/api/v0/organizations"

headers = {
    'X-Cisco-Meraki-API-Key': "secret-api-key",
    'Authorization': "Basic secret-auth-key",
    'User-Agent': "PostmanRuntime/7.18.0",
    'Accept': "*/*",
    'Cache-Control': "no-cache",
    'Postman-Token': "secret-postman-token",
    'Accept-Encoding': "gzip, deflate",
    'Referer': "https://api.meraki.com/api/v0/organizations",
    'Connection': "keep-alive",
    'cache-control': "no-cache"
    }

response = requests.request("GET", url, headers=headers)
print(response.text)

I’m going to skip over the details of how I use the API to connect to the Meraki Dashboard and focus on parsing the JSON response.

The way I’m getting the JSON data is by sending a get request to https://dashboard.meraki.com/api/v0/organizations

The response is assigned to the variable response and printed as a string using print(response.text). The output is:

[{"id":"578149602163689118","name":"Test Org [creation] 1551299469","url":"https://n27.meraki.com/o/Zq76qaB/manage/organization/overview"},{"id":"578149602163689494","name":"Your Momma's Network","url":"https://n27.meraki.com/o/OLu9EcB/manage/organization/overview"},{"id":"578149602163689487","name":"Your org","url":"https://n27.meraki.com/o/rrHTibB/manage/organization/overview"},{"id":"578149602163688579","name":"Your org","url":"https://n27.meraki.com/o/iVpEldB/manage/organization/overview"},{"id":"578149602163689106","name":"test 2","url":"https://n27.meraki.com/o/hDppEbB/manage/organization/overview"},{"id":"578149602163689090","name":"My org","url":"https://n27.meraki.com/o/niwmebB/manage/organization/overview"}
**truncated**

The presentation isn’t very useful and barely readable.

To make the data readable, I’ll utilize the JSON library within Python, used for encoding/decoding JSON.

Key to this library is when a string is loaded into the JSON decoder, it will convert JSON objects into Python dictionaries and JSON arrays into Python lists!

That’s crucial to remember. It gives us flexibility of iterating through the data. It had me confused for a while as I tried to figure out how to parse out the names of all the organizations into a single list.

First, we’ll need to add import json at the top of the script.

If I wanted to, I could make it easier to read with some sorting and indentation by adding print(json.dumps(r, sort_keys=True, indent=4, separators=(',', ':')))

The output is much clearer:

[
    {
        "id":"578149602163688921",
        "name":"Your org",
        "url":"https://n27.meraki.com/o/Lx9DRcB/manage/organization/overview"
    },
    {
        "id":"578149602163689112",
        "name":"Default Test Organization",
        "url":"https://n27.meraki.com/o/oHWaZbB/manage/organization/overview"
    },
    {
        "id":"578149602163689040",
        "name":"Lab 4",
        "url":"https://n27.meraki.com/o/CBPpjbB/manage/organization/overview"
    }
      ** TRUNCATED **
]

We’re printing the JSON data to the screen but we’re going to serialize that data using json.dumps and sort it with indentation.

Now we’re going to comment out print(response.text) since we no longer need the print out of the raw JSON format.

Below #print(response.text) we will turn the JSON data into a Python object and assign that to variable r:

r = json.loads(response.text)

What I’d like to do is parse the JSON data and only provide a list of Organization names I have access to.

At the bottom of the script I created a for loop to iterate through the list and only output the organization names:

for organizations in r:
    print(organizations['name'])

It took me a few hours to get this point as I learned how I took the JSON data and created a list of dictionaries.

The end result is below:

Your org
Default Test Organization
Lab 4
** TRUNCATED **

The final script is below.

import requests
import json

url = "https://dashboard.meraki.com/api/v0/organizations"

headers = {
    'X-Cisco-Meraki-API-Key': "secret-api-key",
    'Authorization': "Basic secret-basic-key",
    'User-Agent': "PostmanRuntime/7.18.0",
    'Accept': "*/*",
    'Cache-Control': "no-cache",
    'Postman-Token': "secret-postman-token",
    'Accept-Encoding': "gzip, deflate",
    'Referer': "https://api.meraki.com/api/v0/organizations",
    'Connection': "keep-alive",
    'cache-control': "no-cache"
    }

response = requests.request("GET", url, headers=headers)
#print(response.text)

r = json.loads(response.text)

# UNCOMMENT BELOW TO SHOW THE JSON OUTPUT PRETTIFIED
# print(json.dumps(r, sort_keys=True, indent=4, separators=(',', ':')))

for organizations in r:
    print(organizations['name’])

Intro to JSON for DevNet

October 25, 2019 By Rowell Leave a Comment

In continuation with the DevNet Associate Challenge, I look at the JSON structured data format. It’s heavily used with Cisco documentation and training. If you’re studying for the Cisco DevNet Associate certification, it will be critical to understand JSON and how it will be used with scripting and APIs.

JSON stands for JavaScript Object Notation. It is a syntax for storing and exchanging data, just like XML and YAML.

When we query a device through a script or software application, we can receive that data in JSON format.

The data being transferred can be used for machine-to-machine interaction. It’s human-readable as well. While we may be used to CLI output, as network engineers, it may not be very useful for machines. Therefore, we have a data structure being used such as JSON.

It appears that Cisco may be settling on JSON as the format to use with their APIs. It may be the desired format for other technologies as well.

Using the Meraki sandbox as an example, we make a request and we receive a response in JSON format below (truncated):

[
    {
        "id": "578149602163689099",
        "name": "Test Org 1551210428",
        "url": "https://n27.meraki.com/o/X7M2PbB/manage/organization/overview"
    },
    {
        "id": "578149602163689479",
        "name": "Your org",
        "url": "https://n27.meraki.com/o/wwBiUbB/manage/organization/overview"
    },
    {
        "id": "549236",
        "name": "DevNet Sandbox",
        "url": "https://n149.meraki.com/o/-t35Mb/manage/organization/overview"
    }
]

In it’s simplest form, JSON consists of key-value pairs. The response above is a request for all organizations my account has access to. What we see is the output in a JSON array format which includes various organizations in their own JSON object.

The entire output is the Organizations array. Within that array are objects such as:

{
        "id": "549236",
        "name": "DevNet Sandbox",
        "url": "https://n149.meraki.com/o/-t35Mb/manage/organization/overview"
    }

The key is identified on the left of the colon and the right side is the value. The syntax is simple:

”key”: “value”

What we do not see in JSON structured data is the support for comments. As I’ve pointed out with XML and YAML, there is the capability to add comments with a #.

When working with networking equipment, I personally find JSON to be easier to parse with the eyes.

JSON | XML | YAML

Which format do you prefer to use? Comment down below👇

XML Basics for DevNet

October 23, 2019 By Rowell Leave a Comment

Understanding the basic concepts of the XML format and the content it contains will be important when dealing with APIs and writing scripts.

I continue my series in studying for the Cisco DevNet Associate certification.

XML stands for Extensible Markup Language and is a way to describe sets of data. Its purpose is to be human readable and to be used to store and transport data.

When it comes to networking, we can use XML to exchange data between systems and also store data. With the use of APIs, we can query network devices and change their settings.

Basic structure of XML as an example:

<network>
    <name>Meraki San Francisco - wireless</name>
    <total_usage_kbytes>1097882601473</total_usage_kbytes>
    <total_user_count>6680</total_user_count>
    <node>
        <mac>88:15:44:60:76:48</mac>
        <name>1.01</name>
        <lat>37.7706845469377</lat>
        <lng>-122.387316962811</lng>
        <is_active>true</is_active>
        <seconds_since_update>11</seconds_since_update>
        <channel>1</channel>
        <last_reported_from>184.23.135.130</last_reported_from>
        <gateway_metric>1</gateway_metric>
        <is_gateway>true</is_gateway>
        <users_in_last_day>9</users_in_last_day>
        <usage_in_last_day_in_kbytes>959578</usage_in_last_day_in_kbytes>
    </node>
</network>

It reads straight forward where you have network as the root element containing child elements such as name and node.

What we can do with this data is manipulate it through scripting or software by sending, receiving, or displaying the data.

XML Structure
Think of XML as a tree-like structure. There are different types of nodes but we should be very comfortable with elements and data.

  • document node
  • elements
  • data
  • processing instructions
  • comments

When we work with XML we must follow some rules so it maintains the correct structure used across systems. It’s rather simple to follow this structure just by looking at the example above.

An XML element must have a start and end-tag enclosed in angle brackets. An example is <Hostname>..</Hostname>. And it is possible to have an empty element but, again, it should have a start-tag and end-tag.

From the example you can see that some XML elements are nested. When nesting elements, they must be closed properly.

So let’s take a look at that example again:

<network>
    <name>Meraki San Francisco - wireless</name>
    <total_usage_kbytes>1097882601473</total_usage_kbytes>
    <total_user_count>6680</total_user_count>
    <node>
        <mac>88:15:44:60:76:48</mac>
        <name>1.01</name>
        <lat>37.7706845469377</lat>
        <lng>-122.387316962811</lng>
        <is_active>true</is_active>
        <seconds_since_update>11</seconds_since_update>
        <channel>1</channel>
        <last_reported_from>184.23.135.130</last_reported_from>
        <gateway_metric>1</gateway_metric>
        <is_gateway>true</is_gateway>
        <users_in_last_day>9</users_in_last_day>
        <usage_in_last_day_in_kbytes>959578</usage_in_last_day_in_kbytes>
    </node>
</network>

<name> is nested under <node>. Nested elements should be closed prior to the element above it. Additionally, the element names are case sensitive.

If we wanted to add comments we can do so with the syntax <!—- this is a comment – >

<device>
    <!--THIS IS A COMMENT-->
    <Hostname>R1</Hostname>
    <IPv4>192.168.1.1</IPv4>
</device>

Here’s an XML example from the Meraki sandbox:

<network>
    <name>Meraki San Francisco - wireless</name>
    <total_usage_kbytes>1097882601473</total_usage_kbytes>
    <total_user_count>6680</total_user_count>
    <node>
        <mac>88:15:44:60:76:48</mac>
        <name>1.01</name>
        <lat>37.7706845469377</lat>
        <lng>-122.387316962811</lng>
        <is_active>true</is_active>
        <seconds_since_update>11</seconds_since_update>
        <channel>1</channel>
        <last_reported_from>184.23.135.130</last_reported_from>
        <gateway_metric>1</gateway_metric>
        <is_gateway>true</is_gateway>
        <users_in_last_day>9</users_in_last_day>
        <usage_in_last_day_in_kbytes>959578</usage_in_last_day_in_kbytes>
    </node>
</network>

DevNet Associate Challenge

October 22, 2019 By Rowell 1 Comment

For over a year I’ve been making excuses when it comes to learning how to code. In the beginning I had chosen Python as my coding language of choice. Now we have the DevNet Associate certification!

With the announcement of the new Cisco DevNet certifications, I’ve kicked around the idea of really taking the plunge to accelerate my learning. I have use cases but have been too “busy” doing other things.

I’ve decided to put myself out there and commit to learning each of the objectives in the Cisco DevNet Associate exam with the intention of taking the exam in February 2020. I’m using this certification study as a way to increase my coding capabilities with Python.

DevNet Certifications
Cisco DevNet Certifications

The decision to pursue the Cisco DevNet Associate makes sense given the work I do with Cisco equipment. All the components are within my grasp.

But there are no published training material available for the DevNet Associate certification. The lack of study guides and other training material should be no barrier. The objectives are clear and the resources to learn those objectives are available on Cisco’s website.

You just have to find it.

But on this blog I’ll be outlining each of the objectives and providing my progress towards achieving the DevNet Associate certification.

My background is in network engineering with a focus in wireless technologies. The knowledge gained from coding and utilizing APIs is substantial.

So that’s my task for the next few months; The DevNet Associate Challenge.

Yeah! DevNet!

Are you studying for the DevNet Associate? Let me know in the comments 👇

YAML Basics for DevNet

October 21, 2019 By Rowell 1 Comment

Taking a look at the Cisco DevNet Associate exam topics (DEVASC 200-901), one of the first objectives is to compare different data formats. Those data formats you should be able to compare are XML, JSON, and YAML.

To start, I decided to take a look at YAML and what it has to offer from a very basic overview. I’m very new when it comes to DevOps or NetDevOps but this will become my notes of sorts as I look into the DevNet Associate certification.

Studying for the Cisco DevNet Associate Certification?

Let me know what your coding experience is in the comments below.

YAML is a human-readable structured data format which stands for Stands for Yet Another Markup Language or now YAML Ain’t Markup Language, depending on where you look 😉 The official documentation page states the latter.

Goals of YAML

  • Data serialization standard for programming languages.
  • Human-readable structured data format.
  • Portable between programming languages.
  • Consistent model.
  • Expressive and extensible.
  • Easy to implement and use.

A quick overview of YAML

  • Writable and readable.
  • Case sensitive
  • Files end in .yaml or .yml
  • Spaces are used instead of tabs (because various tools treat tabs differently)
  • Indentation is used for structure
  • Colons separate key-value pairs
  • Dashes create bullet lists
  • The start of a YAML file begins with three dashes

Data structures:

  • Mappings (hashes/dictionaries)
  • Sequences (arrays/lists)
  • Scalars (strings/numbers)

Working with Blocks
Block collections use indentation for scope and begin each entry on its new line. Block sequences use a dash and space to indicate each entry. Mappings use a colon and space to mark a key-value pair.

To create a comment, you must use a hash tag.

Sequence
A sequence entry will be denoted by a dash -.

 - netsw-idf-01
 - netsw-idf-02
 - netsw-idf-03 

Scalar
A scalar is just a single value. They can be strings, numbers, Boolean, or null values.

# scalars can be quoted
“GigabitEthernet1/0/1”

# scalars can also be unquoted
GigabitEthernet1/0/2 

Mapping Scalars to Sequences
Also creates an array.

routers:
    - rtr-idf-01
    - rtr-idf-02
switches:
    - netsw-idf-01
    - netsw-idf-02

Sample Inventory in YAML

—--
sites:
    - name: san-jose # This is a comment. Begin block 1.
        hosts:
            - hostname: r1
              host: 192.168.1.1
              os: cisco_ios
            - hostname: sw1
              host: 192.168.1.5
              os: cisco_ios
            - hostname: sw2
              host: 192.168.1.6
              os: junos

    - name: san-diego # This is a comment. Begin block 2.
        hosts:
            - hostname: r2
              host: 192.168.2.1
              os: cisco_ios
            - hostname: sw1
              host: 192.168.2.5
              os: cisco_ios

Have we created a list or a dictionary? List items which is a dictionary.

Here’s a sample YAML script used to configure a VLAN on a Cisco switch.

---
- hosts: cisco
  connection: network_cli
  tasks:
    - nxos_vlan:
      vlan_id: 10
      name: STORAGE

Primary Sidebar

Recent Posts

  • Passed Palo Alto Networks Certified Security Administrator (PCNSA)
  • 5 Years Running
  • Q4 2021 and Yearly Income Report
  • I PASSED JNCIA-MistAI
  • Admins and Role-Based Access Control – PCNSA

Categories

  • bschool
  • Certifications
  • Coding
  • DevNet Associate
  • Events
  • Lab
  • Networking
  • Personal
  • Podcasting
  • Professional
  • Reviews
  • Security
  • Short Stories
  • Uncategorized
  • Wireless

Archives

  • May 2022
  • January 2022
  • December 2021
  • November 2021
  • August 2021
  • July 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • November 2018
  • September 2018
  • August 2018

Copyright © 2022 · Written by Rowell Dionicio · You're awesome.

 

Loading Comments...