0

I'm currently using Watsons powerful speech to text API, which returns a JSON(?) on microphone input.

This is part of the code which returns the JSON File:

service.recognizeUsingWebSocket(audio, options, new BaseRecognizeCallback() {
      @Override
      public void onTranscription(SpeechResults speechResults) { 
          System.out.println(speechResults);
      }
    }); 

What I'm currently trying to do, is to get the "transcript" part of the speechResults json (see output), but it doesn't seem to work with the typical json description using a json parser, since the speechResults is not a String.

Do you guys have any ideas how to realize this?

This is the output:

{
  "result_index": 0,
  "results": [
    {
      "final": true,
      "alternatives": [
        {
          "confidence": 0.908,
          "timestamps": [
            [
              "are",
              0.03,
              0.2
            ],
            [
              "you",
              0.2,
              0.36
            ]
          ],
          "transcript": "are you ",
          "word_confidence": [
            [
              "are",
              0.838
            ],
            [
              "you",
              0.982
            ]
          ]
        }
      ]
    }
  ]
}
Sayuri Mizuguchi
  • 5,250
  • 3
  • 26
  • 53
JSt
  • 799
  • 2
  • 10
  • 21

1 Answers1

0

You have to decompose the full object to reach the entries array.

Assuming SpeechResults is already a parsed JSONObject.

SpeechResults.getJSONObject("results").getJSONArray("transcript");

Example I:

//By using javasript json parser
var json = SpeechResults.results; 
System.out.println(json.transcript)

Example II:

        String jsonStr = SpeechResults;
        JSONObject jsonObj = new JSONObject(jsonStr);
        String transcript = jsonObj.getString("transcript");
        System.out.println(transcript);
Community
  • 1
  • 1
Sayuri Mizuguchi
  • 5,250
  • 3
  • 26
  • 53