1

I have set up a bot for Discord that is taking every users' messages and using a sentiment classifier to determine if a message is positive or negative. See the POST request to the text-processing.com API below.

Given a message 'I am extremely happy', the API will return a response which is a string resembling JSON:

{"probability": {"neg": 0.32404484915164478, "neutral": 0.021768879244280313, "pos": 0.67595515084835522}, "label": "pos"}

How can I convert this JSON object to a str, so that I can easily interact with its data?

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return
    with open('data.txt', 'a') as f:
        print(repr(message.content), file=f)
        response = requests.post('http://text-processing.com/api/sentiment/', {
            'text': message.content
        }).json()
        print(response, file=f)
        d = json.loads(response)
        print(d["probability"]["pos"], file=f)
        f.close()

    await bot.process_commands(message)

The error code I am receiving is...

File "/Users/enzoromano/PycharmProjects/NewDiscordBot/NewBot.py", line 44, in on_message
    d = json.loads(response)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 348, in loads
    'not {!r}'.format(s.__class__.__name__))
TypeError: the JSON object must be str, bytes or bytearray, not 'dict'
1
  • s['probability']['pos'] assuming your dictionary above is assigned to s Commented Jul 30, 2018 at 17:08

2 Answers 2

3

Since you are using requests, you can achieve what you ask for by simply parsing the result directly as json instead that as text:

response = requests.post('http://text-processing.com/api/sentiment/', {
    'text': message.content
}).json()
Sign up to request clarification or add additional context in comments.

10 Comments

You are retrieving the data using requests post, which returns a Response. If such response is expected to be a json, you can call its method .json() to convert it to a dictionary directly.
@EnzoRomano Yes, instead of using r.text, use r.json() to automatically parse the JSON into a Python dictionary.
Okay so your answer works but I need to save my data as a str because when I used your code I got.... TypeError: the JSON object must be str, bytes or bytearray, not 'dict'
Could you provide the full exception and the row of code that rise such Exception?
You already convert the response to json with my answer code. If you try to convert it yet again to json it will surely fail.
|
3

Assuming that {"probability": {"neg": 0.32404484915164478, "neutral": 0.021768879244280313, "pos": 0.67595515084835522}, "label": "pos"} really is a string, you can call json.loads on it, and access its values like you would any ordinary dict.

>>> s = """{"probability": {"neg": 0.32404484915164478, "neutral": 0.021768879244280313, "pos": 0.67595515084835522}, "label": "pos"}"""
>>> import json
>>> d = json.loads(s)
>>> d["probability"]["pos"]
0.6759551508483552

3 Comments

Ah so I was wrong, I got the error: TypeError: the JSON object must be str, bytes or bytearray, not 'Response'. Do you know how to change that "response" into a string?
You are handling a requests response object. Check out my answer to convert it directly into a dictionary, using requests own parser.
So every message will have a different rating though? For variable s, the text will always be different... In your answer you have written code for only one situation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.