Skip to content Skip to sidebar Skip to footer

Read Json File From Python

I am trying to read a json file from python script using the json module. After some googling I found the following code: with open(json_folder+json) as json_file: json_dat

Solution 1:

The code is using json as a variable name. It will shadow the module reference you imported. Use different name for the variable.

Beside that, the code is passing file object, while json.loads accept a string.

Pass a file content:

json_data = json.loads(json_file.read())

or use json.load which accepts file-like object.

json_data = json.load(json_file)

Solution 2:

import json
f = open( "fileToOpen.json" , "rb" )
jsonObject = json.load(f)
f.close()

it should seems you are doing in rather complicated way.

Solution 3:

Try like this :-

json_data=open(json_file)
data = json.load(json_data)
json_data.close()

Solution 4:

Considering the path to your json file is set to the variable json_file:

import json

withopen(json_file, "rb") as f:
    json_data = json.load(f)

print json_data

Solution 5:

I Make This....

import urllib2

link_json = "\\link-were\\"
link_open = urllib2.urlopen(link_json) ## Open and Return page.
link_read = link_open.read()           ## Read contains of page.

json = eval(link_read)[0]              ## Transform the string of read in link_read and return the primary dictionary ex: [{dict} <- returnthis] <- remove this

print(json['helloKey'])

Hello World

Post a Comment for "Read Json File From Python"