How to Read JSON File in Python

Posted on

Introduction

As it also exist in the previous article, this article contain the similar content. The previous articles are focusing on how to read file. Either it is reading a text file which exist in this link and also in this link or it is reading a CSV file in this link. So, an article with the title of ‘How to Read Text File in Python’ in this link will show how to read a text file. Furthermore, an article with the title of ‘How to Read Text File per Line in Python ‘ in this link is also showing how to read a text file. Another one, an article with the title of ‘How to Read CSV File in Python’ in this link is also showing how to read a CSV file.

In order to read JSON file aside from using the normal read() method after opening it using open() method, it is possible by utilizing json.load() function. As usual, before it is possible to use the ‘load()’ function, just import the package or the library first. In this context, it is a package or a library with the name of ‘json’. The following is the actual source code for describing the function for reading JSON file and then print the data exist inside it :

How to Read JSON File in Python

In order to perform the process for reading JSON file in Python programming language, the following are the steps to do it :

  1. First of all, just create a JSON file for usage testing. Below is the content of the JSON file as an example :

    {
      "credit_info": {
        "borrower": {
          "borrower_id": "193039439",
          "birth_date": "1968-01-01",
          "first_name": "Jennifer",
          "middle_name": "Marilyn",
          "last_name": " Stone",
          "residence": {
            "street_address": "225 E 22ND ST",
            "city": "New York",
            "state": "NY",
            "postal_code": "10029",
            "borrower_residency_type": "Current"
          }
        }
      }
    }
  2. the first step is just creating a Python programming language to read the JSON file. Below is the content of that Python programming language script :

    import json
    file_json =  open('myfile.json')
    data_json = json.load(file)
    print(data)
    
  3. The execution of the above Python programming language’s source code or script exist below to demonstrate how to read it. Printing the data is a step for proving that the read process it works. Below is the execution of the Python programming language’s source code exist in the above one :

    C:\python>python
    Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> file_json = open("myfile.json");
    >>> data_json = json.load(file_json);
    >>> print(data_json);
    {'credit_info': {'borrower': {'borrower_id': '193039439', 'birth_date': '1968-01-01', 'first_name': 'Jennifer', 'middle_name': 'Marilyn', 'last_name': ' Stone', 'residence': {'street_address': '225 E 22ND ST', 'city': 'New York', 'state': 'NY', 'postal_code': '10029', 'borrower_residency_type': 'Current'}}}}
    >>>

Leave a Reply