Siddharth Gautam
Siddharth Gautam

Reputation: 115

How do I get "transcript" data from DetailedResponse object?

I used the IBM speech-to-text service to get text from an audio file.

This is what it the data looks like

    {
    "result": {
        "results": [
            {
                "alternatives": [
                    {
                        "confidence": 0.6,
                        "transcript": "state radio "
                    }
                ],
                "final": true
            },
            {
                "alternatives": [
                    {
                        "confidence": 0.77,
                        "transcript": "tomorrow I'm headed to mine nine 
    consecutive big con I'm finna old tomorrow I've got may meet and greet 
    with whoever's dumb enough to line up for that and then on Friday you can 
    catch me on a twitch panel"
                    }
                ],
                "final": true

I tried to turn it into JSON by using

    print(json.dumps(output, indent = 4))

But it gives the error

    TypeError: Object of type DetailedResponse is not JSON serializable

How do I use this data to print "transcript" only?

Upvotes: 0

Views: 174

Answers (1)

chughts
chughts

Reputation: 4737

json.dumps() converts a Python object into a JSON String, which is done by the API samples to log / print the response, but in a quirk of Python 3.7 what made a Python object json serialisable changed.

If you take a look at the TypeError, output is an instance of type DetailedResponse. So you need to modify your code to either using proper object encapsulation

print(json.dumps(output.get_result(), indent = 4))

or because its not a protected property.

print(json.dumps(output.result, indent = 4))

Fortunately output.result is json serialisable.

Upvotes: 1

Related Questions