Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python google-trans-new translate raises error: JSONDecodeError: Extra data:

While working on Google translate API, I found out some times google can't translate anything, while it keeps raising the same exception: Extra data.

I have searched on the internet, I found a theory saying I have been blocked by Google translate somehow, or can be blocked because translation data exceeds the 5k character limit. here is a solution but I don't think it is the proper way to solve it.

My code is not new, it has been around 1 month working properly, but a few days ago, it started raising the error, I that time I don't know how to solve it so I leave it overnight to solve it later, but when I woke up, it is working again, I thought it was an error caused by google or something so I just forget it, but at the same day, around 10 pm, It stops working. I want to say is it's very inconsistent, sometimes it works some times doesn't.

How to reproduce: run the example code from google_trans_new package website with specified python version.

here is my code:

from google_trans_new import google_translator

translator = google_translator()  
translate_text = translator.translate('สวัสดีจีน',lang_tgt='en') 
print(translate_text)

output:

Traceback (most recent call last):
  File "c:/Users/my_name/MyApp.py", line 105, in <module>
    translate_text = translator.translate('สวัสดีจีน',lang_tgt='en')
  File "C:\Users\my_name\AppData\Local\Programs\Python\Python37\lib\site-packages\google_trans_new\google_trans_new.py", line 188, in translate
    raise e
  File "C:\Users\my_name\AppData\Local\Programs\Python\Python37\lib\site-packages\google_trans_new\google_trans_new.py", line 152, in translate
    response = json.loads(response)
  File "C:\Users\my_name\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Users\my_name\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 341, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 371 (char 370)

I have tested on my computer, my Heroku web app(for discord bot and line bot), and my friend's laptop. They all raising the same error.

my environments:

Python version: 3.7.0 (google-trans-new specified >= 3.6)
Heroku Python version: 3.8.10
like image 627
Xia Avatar asked Jul 01 '21 17:07

Xia


People also ask

How to use Google Translate API in Python?

Before you can work with the Google Translate API in Python, you will have to install it. There are two different methods of installing the API. The first method is straight forward. Simply go to terminal and use the pip installer to install the API, as you would for any other Python library.

What is jsondecodeerror in Python?

The Python "json.decoder.JSONDecodeError: Extra data" occurs when we try to parse multiple objects without wrapping them in an array. To solve the error, wrap the JSON objects in an array or declare a new property that points to an array value that contains the objects.

How to pass text from one language to another using googletrans?

To do so, we have to import the Translator class from googletrans module. Next, you have to create an object of the Translator class. Once the Translator class object is created, you will pass the text in source language as a parameter to the translate () method of the Translator () class object, as shown below:

How to reproduce Google_Trans_New with specified Python version?

How to reproduce: run the example code from google_trans_new package website with specified python version. from google_trans_new import google_translator translator = google_translator () translate_text = translator.translate ('สวัสดีจีน',lang_tgt='en') print (translate_text)


Video Answer


1 Answers

There is already an open git issue for this. The workaround for it is:

Change line 151 in google_trans_new/google_trans_new.py which is: response = (decoded_line + ']') to response = decoded_line

You just need to clone the google_trans_new repository and edit line 151 on google_trans_new.py as mentioned above.

Test done using the edited code and your input (สวัสดีจีน): enter image description here

like image 148
Ricco D Avatar answered Nov 14 '22 22:11

Ricco D