I am using this code in javascript to encode one json
a = {'address': "djdjdjdj", "provider": "google"}
{address: 'djdjdjdj', provider: 'google'}
btoa(a)
'W29iamVjdCBPYmplY3Rd'
I am using this code to decode in backend
import json
import base64
encodes_Str = "W29iamVjdCBPYmplY3Rd"
btoa = lambda x:base64.b64decode(x)
atob = lambda x:base64.b64encode(bytes(x, 'utf-8')).decode('utf-8')
decoded = json.loads(btoa(encodes_Str))
print(decoded)
error:
>>> decoded = json.loads(btoa("W29iamVjdCBPYmplY3Rd"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 2 (char 1)
>>>
I am getting above error. How can i get back the json which i encodes in javascript please take a look
Your problem is that you haven't converted your JavaScript object into a string before attempting to base64 encode it. Hence when you decode it in python, you get the string:
[object Object]
In your JavaScript you need to JSON encode the object first i.e.
a = {'address': "djdjdjdj", "provider": "google"}
btoa(JSON.stringify(a))
This will give the string:
"eyJhZGRyZXNzIjoiZGpkamRqZGoiLCJwcm92aWRlciI6Imdvb2dsZSJ9"
which you can then run through
json.loads(base64.b64decode(encodes_Str))
to get:
{'address': 'djdjdjdj', 'provider': 'google'}