0

I use the following code : https://github.com/sararob/ml-talk-demos/blob/master/speech/request.sh to fit my own bash script.

cat <<EOF > $JSONFILENAME
{
  "config": {
    "encoding":"LINEAR16",
    "sampleRateHertz":8000,
    "languageCode": "nl-NL",
    "speechContexts": {
      "phrases": ['']
    },
    "maxAlternatives": 1
  },
  "audio": {
    "content":
    }
}
EOF
base64 $1 -w 0 > $SOUNDFILE.base64
#MYBASE64=$(base64 $1 -w 0)
sed -i $JSONFILENAME -e "/\"content\":/r $SOUNDFILE.base64"
#sed -i $JSONFILENAME -e "/\"content\":/r $MYBASE64"
curl -s -X POST -H "Content-Type: application/json" --data-binary @${JSONFILENAME} https://speech.googleapis.com/v1/speech:recognize?key=$API_KEY

The base64 output is correctly filled in by the sed command, however there are also newlines added.

This is the Google API response :

{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unexpected token.\n\": {\n    \"content\":\nUklGRqTIAgBXQVZFZm10\n                    ^",
    "status": "INVALID_ARGUMENT"
  }
}

How can I make sure the "content" in my JSON-object is a continuous string of base64 ?

user2795648
  • 119
  • 1
  • 11

1 Answers1

1

You should avoid updating JSON data with sed.

If you have valid JSON data (i.e you have to fix the lines "phrases": [] and "content": "", you could use jq instead:

jq ".audio.content = \"$(base64 -w 0 "$1")\"" "$JSONFILENAME"

I don't recommend sed, but in this case where a large entry must be appended, you could try this:

echo \"$(base64 -w 0 "$1")\" > "$SOUNDFILE.base64"
sed -i "$JSONFILENAME" -e "/\"content\":/r $SOUNDFILE.base64"

The google error you receive is likely due to the fact that the string is not double quoted.

oliv
  • 12,690
  • 25
  • 45
  • Tried this but getting error: /usr/bin/jq: Argument list too long. Thus this is the same problem I face when trying my alternative: MYBASE64=$(base64 $1 -w 0) – user2795648 Jun 21 '18 at 13:52
  • @user2795648 Your base64 string is rather long... Try this: `jq --arg a "$(base64 -w 0 "$1")" '.audio.content = $a'` – oliv Jun 21 '18 at 14:02
  • @user2795648 Alternatively you can use `jq --argfile a "$SOUNDFILE.base64" '.audio.content = $a'` – oliv Jun 21 '18 at 14:05
  • No solution. First case : getting same "argument list too long". Second case : "jq: Unknown option --argfile". Yes the base64 is long. This is a sound file of 10seconds. Expected are sound files of several minutes ! – user2795648 Jun 21 '18 at 14:38
  • I've tried putting double quotes inside the sed-command... But the solution with the echo \" \" is great ! – user2795648 Jun 21 '18 at 19:22