0

I have a Json file from which I want to extract particular data. Below is the JSON file:

{
    "results": [ {
        "alternatives": [ {
            "word_confidence": [
                [ "Ryan", 0.335 ],
                [ "how's", 0.589 ],
                [ "the", 1.0 ],
                [ "weather", 1.0 ],
                [ "in", 1.0 ],
                [ "New", 1.0],
                [ "York", 0.989 ],
                [ "today", 0.987 ]
            ],
            "confidence": 0.795,
            "transcript": "Ryan how's the weather in New York today "
        } ],
        "final": true
    } ],
    "result_index": 0
}

Using python, how can I parse this file and get extract "transcript"?

3
  • import json; data=json.loads(json_string) Commented Feb 26, 2017 at 6:50
  • @stephen I used ' import json; data = json.loads(json_string)' which returns '{u'results': [{u'alternatives': [{u'transcript': u"Ryan how's the weather in New York today ", u'confidence': 0.795, u'word_confidence': [[u'Ryan', 0.335], [u"how's", 0.589], [u'the', 1.0], [u'weather', 1.0], [u'in', 1.0], [u'New', 1.0], [u'York', 0.989], [u'today', 0.987]]}], u'final': True}], u'result_index': 0}' now I have to extract the data for key value "transcript".. how to do this Commented Feb 26, 2017 at 7:38
  • Found solution for this data = json.loads(json_data) text_data = data["results"][0]["alternatives"][0]["transcript"] Commented Feb 26, 2017 at 11:51

1 Answer 1

2

To convert the json string to a dict, use json.loads(). Then to get the transcript, just reference into the dict, like:

Code:

import json
data = json.loads(json_string)
transcript = data['results'][0]['alternatives'][0]['transcript']

Test Data:

json_string = """
{
    "results": [ {
        "alternatives": [ {
            "word_confidence": [
                [ "Ryan", 0.335 ],
                [ "how's", 0.589 ],
                [ "the", 1.0 ],
                [ "weather", 1.0 ],
                [ "in", 1.0 ],
                [ "New", 1.0],
                [ "York", 0.989 ],
                [ "today", 0.987 ]
            ],
            "confidence": 0.795,
            "transcript": "Ryan how's the weather in New York today "
        } ],
        "final": true
    } ],
    "result_index": 0
}
"""
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.