"results": [
{
"result_index": 0,
"results": [
{
"final": true,
"alternatives": [
{
"transcript": "THE TEXT ",
"confidence": 0.2958
}
]
}
]
}
],
"status": "completed"
I want the value of transcript in this Json, basically i want :THE TEXT
The parse i tried and didn't work :
def parse(obj):
text=[]
if 'results' in obj:
results = obj['results']
for result in results:
if 'results' in results:
results = results['results']
if 'alternatives' in result:
alternatives = result['alternatives']
assert len(alternatives) == 1
alternative = alternatives[0]
text.append(alternative['transcript'])
return ' '.join(text).lstrip().rstrip()
What would the correct parse function to use be
Why not use the json package? It'd make your life easier, use:
import json
Then, from thereon:
data2 = json.loads('''{"results": [
{
"result_index": 0,
"results": [
{
"final": true,
"alternatives": [
{
"transcript": "THE TEXT ",
"confidence": 0.2958
}
]
}
]
}
],
"status": "completed"}''')
print(data2['results'][0]['results'][0]['alternatives'][0]['transcript'])
First of all, your JSON isn't even valid, you have to enclose it in curly brackets. Next, as JSON.loads expects a string, you have to wrap it in '''
. Another way'd be that you declare your JSON as a var, then call new_var = JSON.dumps(your_json_var)
, and then JSON.loads(new_var)
, and then call the same print function as above.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.