Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»How to Read, Write & Parse JSON in Python

    How to Read, Write & Parse JSON in Python

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

    Advertisement

    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

    Related Posts

    PHP Arrays: A Beginner’s Guide

    How to Create and Read List in Python

    What are the Python Static Variables

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to List Manually Installed Packages in Ubuntu & Debian
    • 10 Bash Tricks Every Developer Should Know
    • How to Validate Email Address in JavaScript
    • Firewalld: Common Firewall Rules and Commands
    • 12 Apk Commands in Alpine Linux Package Management
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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