I'm making a chatbot to detect toxicity, using the google perspective API, It responds with a dictionary shown below.
{
"attributeScores": {
"TOXICITY": {
"spanScores": [
{
"begin": 0,
"end": 11,
"score": {
"value": 0.05588363,
"type": "PROBABILITY"
}
}
],
"summaryScore": {
"value": 0.05588363,
"type": "PROBABILITY"
}
}
},
"languages": [
"en"
],
"detectedLanguages": [
"en"
]
}
How can I format the above json to get the first "value": 0.05588363 as either a string or int? Help would be much appreciated!
This is my code:
from googleapiclient import discovery
import json
import os
API_KEY= os.getenv('API_KEY')
service = discovery.build('commentanalyzer', 'v1alpha1', developerKey=API_KEY)
analyze_request = {
'comment': { 'text': 'sample text' },
'requestedAttributes': {'TOXICITY': {}}
}
response = service.comments().analyze(body=analyze_request).execute()
val = (json.dumps(response, indent=2))
print(val)
final = val["attributeScores"]["TOXICITY"]["spanScores"][0]["score"]["value"]
print(final)