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?
If you have a JSON string, you can parse it by using the json.loads() method. The result will be a Python dictionary.
To convert Python JSON string to Dictionary, use json. loads() function. Note that only if the JSON content is a JSON Object, and when parsed using loads() function, we get Python Dictionary object. JSON content with array of objects will be converted to a Python list by loads() function.
To read the files, we use read_json() function and through it, we pass the path to the JSON file we want to read. Once we do that, it returns a “DataFrame”( A table of rows and columns) that stores data.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With