user7377021
user7377021

Reputation: 333

How to read a json file and return as dictionary in Python

Trying to read a json file and return as dictionary:

  def js_r(filename):
  with open('num.json', 'r')as f_in:
  json_d = f_read()

How to return the dict function?

Upvotes: 33

Views: 37762

Answers (1)

tdelaney
tdelaney

Reputation: 77347

Use the json module to decode it.

import json

def js_r(filename: str):
    with open(filename) as f_in:
        return json.load(f_in)

if __name__ == "__main__":
    my_data = js_r('num.json')
    print(my_data)

Upvotes: 49

Related Questions