json to base64 python

Solutions on MaxInterview for json to base64 python by the best coders in the world

showing results for - "json to base64 python"
Maite
08 Nov 2016
1>>> import json
2>>> import base64
3>>> d = {"alg": "ES256"} 
4>>> s = json.dumps(d)  # Turns your json dict into a str
5>>> print(s)
6{"alg": "ES256"}
7>>> type(s)
8<class 'str'>
9>>> base64.b64encode(s)
10Traceback (most recent call last):
11  File "<stdin>", line 1, in <module>
12  File "/usr/lib/python3.2/base64.py", line 56, in b64encode
13    raise TypeError("expected bytes, not %s" % s.__class__.__name__)
14TypeError: expected bytes, not str
15>>> base64.b64encode(s.encode('utf-8'))
16b'eyJhbGciOiAiRVMyNTYifQ=='
17
Anastasia
06 Apr 2018
1data = '{"hello": "world"}'
2enc = data.encode()  # utf-8 by default
3print base64.encodestring(enc)