Home > JSON Tutorial

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 an array which 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”]
}
"""
Comments (19) Comments
  1. Anonymous
    July 18th, 2020

    Good to see about SDN .. Can you put some stuff on SDWAN as well

  2. 9tut
    July 19th, 2020

    @Anonymous: You can find tutorials about SDWAN at: certprepare.com.

  3. siri
    September 1st, 2020

    @9TUT taking membership helps us to pass CCNA ?

  4. 9tut
    September 2nd, 2020

    @siri: Yes, you can pass this exam with our Premium Membership.

  5. fake
    September 2nd, 2020

    @siri: Yes, you can pass this exam with our Premium Membership.

  6. Algernon
    September 2nd, 2020
  7. Andrew
    January 28th, 2021

    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. ?

  8. MoG
    February 1st, 2021

    @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.

  9. Anonymous
    September 29th, 2021

    I found these information useful, but how does premium membership can help me pass CCNA exam??

  10. Andy
    June 20th, 2023

    Thanks a lot

  11. Anonymous
    June 30th, 2023

    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

  12. Clifford Attaglo
    July 4th, 2023

    I took the exams today and passed everything. I mean all that i go here was on the exams. Thanks 9tut.

  13. CCNA LAB SIM
    September 5th, 2023

    dears can i know the recent CCNA LAB related question plse if any ??

  14. Jama saleh
    October 3rd, 2023

    @9TUT , I renewed my membership yesterday but it says your membership is expired, can you please help me with that ?

  15. 9tut
    October 3rd, 2023

    @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!

  16. David Q
    October 11th, 2023

    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

  17. Anonymous
    October 11th, 2023

    @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

  18. 9tut
    October 11th, 2023

    @Anonymous: We see you created two accounts so please try logging in again with the second account.

  19. Anonymous
    October 20th, 2023

    how many question are on the big composite quiz?

Add a Comment