This question already has answers here:
Reading JSON from a file [duplicate]
(7 answers)
Closed 4 years ago.
I need to get a particular key:value from a json file.
Code used to open json file :
import json
data = repr(open('file.json', 'rb').read())
print(data)
Output( Json file) :
b'b\'{"image":{"width":750,"height":1127,"orientation":1},"objects":[{"type":"face","boundingBox":{"x":132,"y":180,"height":619,"width":513},"landmarks":{"faceContour":[[162,375],[162,440],[165,504],[175,568],[193,628],[226,686],[265,740],[313,784],[373,799],[436,793],[494,758],[543,712],[586,660],[616,601],[631,534],[639,468],[645,400]],"noseBridge":[[387,396],[385,441],[381,485],[378,532]],"noseBall":[[322,547],[350,560],[380,572],[412,563],[442,554]],"eyebrowRight":[[419,348],[466,329],[516,325],[564,339],[595,377]],"eyebrowLeft":[[197,358],[224,325],[269,315],[316,325],[358,347]],"eyeRight":[[448,419],[479,404],[510,405],[539,416],[511,425],[480,424]],"eyeRightCenter":[[495,416]],"eyeLeft":[[242,402],[269,393],[299,396],[329,412],[297,414],[267,413]],"eyeLeftCenter":[[284,405]],"mouthOuter":[[269,596],[310,598],[349,598],[379,606],[411,601],[456,606],[502,609],[456,663],[412,684],[378,687],[345,680],[307,652]],"mouthInner":[[284,603],[349,610],[379,615],[411,613],[487,614],[412,652],[379,656],[348,648]]},"attributes":{"gender":"male","genderConfidence":0.8385,"age":24,"ageConfidence":0.8419,"emotion":"happiness","emotionConfidence":1.0,"emotionsAll":{"neutral":0.0,"sadness":0.0,"disgust":0.0,"anger":0.0,"surprise":0.0,"fear":0.0,"happiness":1.0},"pose":{"pitch":-5.7105,"roll":0.2941,"yaw":1.8646},"race":{"asian":0.0002,"black":0.0001,"white":0.9989},"eyewear":{"sunglasses":0.0,"glasses":0.0},"hair":{"color":{"blond":0.0,"black":0.9994,"brown":0.0},"cut":{"short":0.9999,"long":0.0,"bald":0.0},"facial":{"beard":0.0,"mustache":0.0,"stubble":0.2543}},"frontal":true}},{"type":"person","boundingBox":{"x":20,"y":60,"height":1053,"width":696}}],"requestId":"1239be0c782440bd828cee21dbc2baa1"}\'\r\n'
From this Json file, I need to extract a particular key:value. For example, I need to get the emotionsAll (Key) from the file.
I tried using :
print(data['emotionsAll'][0]['neutral'])
error i got :
Traceback (most recent call last): File "C:\Users\HP\Downloads\Final Project\getfile.py", line 6, in <module>
print(data['emotionsAll'][0]['neutral']) TypeError: string indices must be integers
So, Is there any other way to get a particular key:value ?
Try using the json package
Contents of file.json
{"image":{"width":750,"height":1127,"orientation":1},"objects":[{"type":"face","boundingBox":{"x":132,"y":180,"height":619,"width":513},"landmarks":{"faceContour":[[162,375],[162,440],[165,504],[175,568],[193,628],[226,686],[265,740],[313,784],[373,799],[436,793],[494,758],[543,712],[586,660],[616,601],[631,534],[639,468],[645,400]],"noseBridge":[[387,396],[385,441],[381,485],[378,532]],"noseBall":[[322,547],[350,560],[380,572],[412,563],[442,554]],"eyebrowRight":[[419,348],[466,329],[516,325],[564,339],[595,377]],"eyebrowLeft":[[197,358],[224,325],[269,315],[316,325],[358,347]],"eyeRight":[[448,419],[479,404],[510,405],[539,416],[511,425],[480,424]],"eyeRightCenter":[[495,416]],"eyeLeft":[[242,402],[269,393],[299,396],[329,412],[297,414],[267,413]],"eyeLeftCenter":[[284,405]],"mouthOuter":[[269,596],[310,598],[349,598],[379,606],[411,601],[456,606],[502,609],[456,663],[412,684],[378,687],[345,680],[307,652]],"mouthInner":[[284,603],[349,610],[379,615],[411,613],[487,614],[412,652],[379,656],[348,648]]},"attributes":{"gender":"male","genderConfidence":0.8385,"age":24,"ageConfidence":0.8419,"emotion":"happiness","emotionConfidence":1.0,"emotionsAll":{"neutral":0.0,"sadness":0.0,"disgust":0.0,"anger":0.0,"surprise":0.0,"fear":0.0,"happiness":1.0},"pose":{"pitch":-5.7105,"roll":0.2941,"yaw":1.8646},"race":{"asian":0.0002,"black":0.0001,"white":0.9989},"eyewear":{"sunglasses":0.0,"glasses":0.0},"hair":{"color":{"blond":0.0,"black":0.9994,"brown":0.0},"cut":{"short":0.9999,"long":0.0,"bald":0.0},"facial":{"beard":0.0,"mustache":0.0,"stubble":0.2543}},"frontal":true}},{"type":"person","boundingBox":{"x":20,"y":60,"height":1053,"width":696}}],"requestId":"1239be0c782440bd828cee21dbc2baa1"}
Code:
import json
with open('file.json', 'rb') as fp:
json_data = json.load(fp)
print(json_data['objects'][0]['attributes']['emotionsAll']['neutral'])
A couple things:
1 - the emotionsAll key is within the objects key, first element in the list [0], attributes key
2 - Your json file was written with the bytes prefix, so when it's being read, it starts your string with b'. You can either a) have that file written without that mode by decoding/encoding, or just manipulate that string.
import json
data = repr(open('file.json', 'rb').read())
data = data.split('{', 1)[-1]
data = data.rsplit('}', 1)[0]
data = ''.join(['{', data, '}'])
jsonObj = json.loads(data)
print(jsonObj['objects'][0]['attributes']['emotionsAll']['neutral'])
Output:
print(jsonObj['objects'][0]['attributes']['emotionsAll']['neutral'])
0.0
Related
The goal is to open a json file or websites so that I can view earthquake data. I create a json function that use dictionary and a list but within the terminal an error appears as a invalid argument. What is the best way to open a json file using python?
import requests
`def earthquake_daily_summary():
req = requests.get("https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson")
data = req.json() # The .json() function will convert the json data from the server to a dictionary
# Open json file
f = open('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson')
# returns Json oject as a dictionary
data = json.load(f)
# Iterating through the json
# list
for i in data['emp_details']:
print(i)
f.close()
print("\n=========== PROBLEM 5 TESTS ===========")
earthquake_daily_summary()`
You can immediately convert the response to json and read the data you need.
I didn't find the 'emp_details' key, so I replaced it with 'features'.
import requests
def earthquake_daily_summary():
data = requests.get("https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson").json()
for row in data['features']:
print(row)
print("\n=========== PROBLEM 5 TESTS ===========")
earthquake_daily_summary()
This question already has answers here:
How do I read a json file into python?
(2 answers)
Closed 3 years ago.
I am trying to get JSON into my python script, but it does not seem to work.
I have tried everything i can come up with. I really dont' know what else to try, any tip is appriciated
jsonfile = 'D://python//tracker4R101-master//firefly-
master//countr_phonecode.json'
dial_json = json.loads(jsonfile)
dial_code = dial_json['dial_code']
country_code = obj['country_code']
I expect the output to be something like
Dial Code: (dial code)
The error message is:
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
If you need further code or explaning just let me know, i rarely post on stackoverflow, so i don't know what information to provide
There are two functions json.load and 'json.loads.
loadlets you pass a file like object, whileloads` lets you pass a json encoded string.
So if you would like to read from a file your code should be something like:
jsonfile_path = 'D://python//tracker4R101-master//firefly-master//countr_phonecode.json'
# open the file
with open(jsonfile_path, 'r') as jsonfile:
dial_json = json.load(jsonfile)
dial_code = dial_json['dial_code']
country_code = obj['country_code']
You could also read the file object and pass the string like json object to the library:
jsonfile_path = 'D://python//tracker4R101-master//firefly-master//countr_phonecode.json'
# open the file and read its contents
with open(jsonfile_path, 'r') as jsonfile:
jsonfile_contents = jsonfile.read()
dial_json = json.loads(jsonfile_contents)
dial_code = dial_json['dial_code']
country_code = obj['country_code']
https://docs.python.org/2/library/json.html#json.load
I have a list of 155 json tags and a json file associated with it. The idea is to parse the json file using the predetermined tags and if one of the tags is not available in the json string then use null.
I am using python 2.7 and native json parser. I need to convert the json in to csv. The problem here is am trying to extract json fields using tag from a file but I am having issues evaluating the tags. I am getting a key error.
I have a list of json schema like below.
json Schema file:
jsonTag.tagb,csv_columnA
jsonTag2.Id,csv_columnB
jsonTag2.Id.description,csv_columnC
I want to loop through the above and get all the json tags.
Use the json tag to extract from the json file. the above is just an example we have around 155 tags and complext nested json.
Below is the method I have in python which gets a schema (tags) from a different method using an ordered Dict format which I need to use to extract the json
def write_to_csv(self):
myvars = self.generate_schema() #gets schema in orderedDict key value parir
header = []
values = []
with open('sample_json.json','r') as json_file: #read json file row by row json string
json_string = json_file.read().splitlines()
for json_line in json_string:
json_parsed=json.loads(json_line) #parse each row
for key, value in myvars.items():
values.append(json_parsed[value]) #evalueat the tag and append to an array which i will use it to do csv write row.
print values
Below is the error i am getting now.
OUTPUT:
Traceback (most recent call last):
File "./parse_nested_json.py", line 86, in <module>
main()
File "./parse_nested_json.py", line 83, in main
parse.write_to_csv()
File "./parse_nested_json.py", line 63, in write_to_csv
values.append(json_parsed[value.strip('\"')])
KeyError: "['trackingEvent']['barcode']"
Expected output:
csv
csv_columnA,csv_columB,csv_columnC
1223,abc,aaa
234,asd,ssaa
..etc
Thanks in advance
This question already has answers here:
Why can't Python parse this JSON data? [closed]
(3 answers)
Closed 5 years ago.
I have a dictionary in python. I want to modify that dictionary and then save the dictionary to an external file so that when I load up the python program again it grabs the dictionary data from the external file.
class Data:
"""
Data handling class to save
and receive json data, parent
of User for data purposes.
"""
def saveData(data, file):
with open(file, 'r+') as dataFile:
dataFile.write(json.dumps(data))
def getData(file):
with open(file, 'r+') as dataFile:
return json.loads(dataFile.readline())
def deleteContent(file):
file.seek(0)
file.truncate()
But when I write to the file and then try to read it reads it as a string and I can't use the read data to set a dictionary. How can I get data in a dictionary from an external JSON file as dictionary data, not string data?
data = Data.getData("chatbotData.json")
dataDict = data
dataDict["age"] = 2
Here is what I want to do with the data and I get this error:
TypeError: 'str' object does not support item assignment
Let's create a dictionary:
>>> d = {'guitar':'Jerry', 'drums':'Mickey' }
Now, let's dump it to a file:
>>> import json
>>> json.dump(d, open('1.json', 'w'))
Now, let's read it back in:
>>> json.load(open('1.json', 'r'))
{'guitar': 'Jerry', 'drums': 'Mickey'}
Taking better care of file handles
The above illustrates the json module but was sloppy about closing files. Better:
>>> with open('1.json', 'w') as f:
... json.dump(d, f)
...
>>> with open('1.json') as f:
... json.load(f)
...
{'guitar': 'Jerry', 'drums': 'Mickey'}
This question already has answers here:
HTTPResponse object -- JSON object must be str, not 'bytes'
(4 answers)
Closed 7 years ago.
I am new to python. I am using python 3.x. I have tried to correct this code many times but I am getting few error messages. Can someone please help to correct me with the code?
import urllib.request as urllib2
#import urllib2
#import urllib2
import json
def printResults(data):
#Use the json module to load the string data into a directory
theJSON = json.loads(data)
#Now we can access the contents of json like any other python object
if "title" in theJSON["metadata"]:
print (theJSON["metadata"]["title"])
def main():
#Define the varible that hold the source of the Url
urlData= "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson"
#Open url and read the data
webUrl= urllib2.urlopen(urlData)
#webUrl= urllib.urlopen(urldata)
print (webUrl.getcode())
if (webUrl.getcode() == 200):
data= webUrl.read()
#Print our our customized result
printResults(data)
else:
print ("Received an error from the server, can't retrieve results " + str(webUrl.getcode()))
if __name__== "__main__":
main()
Here are the errors that I am getting:
Traceback (most recent call last):
File "C:\Users\bm250199\workspace\test\JSON_Data.py", line 30, in <module>
main()
File "C:\Users\bm250199\workspace\test\JSON_Data.py", line 25, in main
printResults(data)
File "C:\Users\bm250199\workspace\test\JSON_Data.py", line 8, in printResults
theJSON = json.loads(data)
File "C:\Users\bm250199\AppData\Local\Programs\Python\Python35-32\lib\json\__init__.py", line 312, in loads
s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'
Just have to tell python to decode the bytes object it got into a string.
This can be done by using the decode function.
theJSON = json.loads(data.decode('utf-8'))
You could make the function more robust by adding an if condition like:
def printResults(data):
if type(data) == bytes: # Convert to string using utf-8 if data given is bytes
data = data.decode('utf-8')
#Use the json module to load the string data into a directory
theJSON = json.loads(data)
#Now we can access the contents of json like any other python object
if "title" in theJSON["metadata"]:
print (theJSON["metadata"]["title"])