Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 431
user7377021 Avatar asked Jan 05 '17 02:01

user7377021


People also ask

Can we convert JSON to dictionary in Python?

If you have a JSON string, you can parse it by using the json.loads() method. The result will be a Python dictionary.

How do I convert a JSON response to a 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.

How do you read a JSON file as a DataFrame in Python?

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.


1 Answers

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)
like image 101
tdelaney Avatar answered Sep 21 '22 12:09

tdelaney