Reading a JSON file from the Google Drive in the Google Colab


Create a JSON file in the Google Drive at a location inside "My Drive".

We will call it 'properties.json'. It has the following contents:

{
    "firstname": "Ashish",
    "lastname": "Jain",
    "country": "India"
}

Next, create "Google Colaboratory" (it will be an .ipynb file) in the Drive.

Follow this sequence: New -> More -> Google Colaboratory

Write this code in the first cell: CELL 1# from google.colab import drive drive.mount('/content/drive') In the output, you get an instruction to follow and authenticate the access to a Drive using an access code. OUTPUT: Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=9... Enter your authorization code: ·········· Mounted at /content/drive CELL 2# # load json path_to_json_file = "/content/drive/My Drive/Colab Notebooks/files_1_1/properties.json" json_file = open(path_to_json_file, 'r') loaded_json = json_file.read() json_file.close() print(loaded_json) OUTPUT: { "firstname": "Ashish", "lastname": "Jain", "country": "India" } CELL 3# import json with open(path_to_json_file, "r") as content: print(json.load(content)) OUTPUT: { 'firstname': 'Ashish', 'lastname': 'Jain', 'country': 'India' }

No comments:

Post a Comment