Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»How to Read, Write & Parse JSON in Python

    How to Read, Write & Parse JSON in Python

    RahulBy RahulJuly 9, 20213 Mins Read

    JavaScript Object Notation aka JSON is a very lightweight standard data representation format. It is used for storing and transporting data. It was derived from Javascript but is now language-independent. It was created with the intention of making a text-based interchange format that is easily readable by humans.

    JSON is compatible with most programming languages and can easily be integrated as they have built-in functionalities to read, write and parse JSON.

    JSON is commonly used across the computing world for APIs and for Config files of different programs such as games. It is also used to transfer data from a server to the client and vice versa.

    In this post, we will go through a guide on how to read, write and parse JSON in python. Python has a built-in package named JSON which can be used to manipulate JSON data.

    How to Convert JSON to Python Dictionary (Parse JSON)

    We can use the json.loads() method to parse JSON into a Python dictionary. We have to provide the JSON in string format to the json.loads() function because this function takes the string and converts the string into a Python dictionary.

    In the example given below, the employee is a JSON string while the employee-dict is a python dictionary.

    1
    2
    3
    4
    5
    6
    import json
     
    employee = '{"First_Name": "John", "Second_Name": "Doe", "id": "01", "Department": "Health"}'
    employee_dict = json.loads(employee)
     
    print(employee_dict)

    Output

    Python Convert JSON to Dictionary

    How to Convert a Python Dictionary to a JSON String

    In the previous section, we learned how to convert a JSON string to a Python dictionary. Now we will do the opposite and convert the Python dictionary into a JSON string.

    1
    2
    3
    4
    5
    6
    import json
     
    employee_dict = {'First_Name': 'John', 'Second_Name': 'Doe', 'id': '01', 'Department': 'Health'}
    employee = json.dumps(employee_dict)
     
    print(employee)

    Output
    Python Convert Dictionary toJSON

    How to Read a JSON File

    We can use the json.load() method to read a JSON object, in Python.

    First, created a data.json file with following content:

    cat /home/user/data.json 
    
    {"First_Name": "John", "Second_Name": "Doe", "id": "01", "Department": "Health"}
    

    Then run the below program to data from JSON file and print on the screen.

    1
    2
    3
    4
    5
    6
    import json
     
    with open('/home/rahul/data.json') as f:
      employee_data= json.load(f)
     
    print(employee_data)

    Output
    Python Read JSON File

    How to Write JSON to a File using Python

    We can use the json.dump() method to write JSON to a .json file.

    The program given below creates a new file named employee.json. If the file already exists then it only opens the file in ‘w’ mode. The ‘w’ mode means that the file is opened in write mode.

    Then json.dump() converts the python dictionary “employee_dict” into a JSON string and writes it into the json file.

    1
    2
    3
    4
    5
    6
    import json
     
    employee_dict = {"First_Name": "John", "Second_Name": "Doe", "id": "01", "Department": "Health"}
     
    with open('employee.json', 'w') as json_file:
      json.dump(employee_dict, json_file)

    Output:

    Python Write JSON File

    Conclusion

    JSON has become the most common method to store and transfer data in recent times. Its ease of use has made it wildly popular among developers.

    In this write-up we have learned json.loads(), json.dumps(), json.load(), and json.dump() methods. These methods help us manipulate, read, write and parse JSON in python.

    json Python
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleChattr Command in Linux with Examples
    Next Article How to Force User to Change Password at Next Login in Linux

    Related Posts

    How to accept user input in Python

    2 Mins Read

    What is difference between var, let and const in JavaScript?

    3 Mins Read

    Setup Selenium with Python and Chrome on Ubuntu & Debian

    Updated:June 20, 20224 Mins Read

    Setup Selenium with Python and Chrome on Fedora

    Updated:June 18, 20223 Mins Read

    How to Replace String in JavaScript

    Updated:June 13, 20222 Mins Read

    Creating Python Virtual Environment on Windows

    Updated:June 3, 20222 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    • What is difference between var, let and const in JavaScript?
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.