JSON Tutorial
JavaScript Object Notation (JSON) is a human readable and very popular format used by web services, programming languages (including Python) and APIs to read/write data. JSON is also a subject of the CCNA 200-301 so in this article we will learn some basic knowledge of JSON and how to use Python to process JSON.
JSON syntax structure:
+ uses curly braces {}
to hold objects and square brackets []
to hold arrays
+ JSON data is written as key/value pairs
+ A key/value pair consists of a key (must be a string in double quotation marks ""
), followed by a colon :
, followed by a value. For example: “name”:”John”
+ Each key must be unique
+ Values must be of type string, number, object, array, boolean or null
+ Multiple key/value within an object are separated by commas ,
JSON can use arrays. Arrays are used to store multiple values in a single variable. For example:
{ “name”:”John”, “age”:30, “cars”:[ “Ford”, “BMW”, “Fiat”] }
In the above example, “cars” is the key which is assigned a JSON array of strings. They contains three values “Ford”, “BMW” and “Fiat”.
If we have a JSON string, we can convert (parse) it into Python by using the json.loads()
method, which returns a Python dictionary:
import json myvar = '{“name”:”John”,“age”:30,“cars”:[ “Ford”, “BMW”, “Fiat”]}' parse_myvar = json.loads(myvar) print(parse_myvar["cars"][0])
The result:
Ford
Note:
+ Python comes with a built-in package called json for encoding and decoding JSON data so we need to “import json” first
+ If you want to write a variable in multiple lines or with special characters (including NEWLINEs, TABs) then you should use triple quotes """
. For example the “myvar” variable above can be written as follows:
myvar = """ { “name”:”John”, “age”:30, “cars”:[ “Ford”, “BMW”, “Fiat”] } """
Good to see about SDN .. Can you put some stuff on SDWAN as well
@Anonymous: You can find tutorials about SDWAN at: certprepare.com.
@9TUT taking membership helps us to pass CCNA ?
@siri: Yes, you can pass this exam with our Premium Membership.
@siri: Yes, you can pass this exam with our Premium Membership.
All pdf workbooks lab from Pnetlab :https://drive.google.com/drive/mobile/folders/19X9O8WoIrNASof5sW-9vD201R-eyT4-e?usp=sharing
Sorry basic question – for the Python result of “Ford” above, how do you write the JSON script to output other values into Python eg. BMW or Fiat. ?
@Andrew >>print(parse_myvar[“cars”][0])<< is the important line for the output. The zero within the output modifire argument does distinguish which part of the array [Ford,BWM;Fiat] is shown. The array positions eqaul the following:
Ford = position 0
BWM= position 1
Fiat = position 2
So if you change the zero in the last line, to any of the numbers above, you would recive your desired date.
I found these information useful, but how does premium membership can help me pass CCNA exam??
Thanks a lot
Hi team
How are doing?
Is there some reference for PMP material
Could you please send it to me at: exam23prep at gmail dot com
Best Regards
I took the exams today and passed everything. I mean all that i go here was on the exams. Thanks 9tut.
dears can i know the recent CCNA LAB related question plse if any ??
@9TUT , I renewed my membership yesterday but it says your membership is expired, can you please help me with that ?
@Jama saleh: If your problem still exists, please send us an email at support@9tut.com with your username so that we can help you!
I renewed my membership for two months on October 3rd and today it says your membership has expired, could you help me with that? I am attaching payment capture
my user is fdespinoza
@9TUT I renewed my membership for two months on October 3rd and today it says your membership has expired, could you help me with that? I am attaching payment capture
my user is fdespinoza
@Anonymous: We see you created two accounts so please try logging in again with the second account.
how many question are on the big composite quiz?
Hi, In the first example.Does “cars” is the an array not a key?
@chin: “cars” is the key. We updated that statement.