What I was addicted to with json.dumps in Python base64 encoding

Introduction

You may want to return base64-encoded data when creating APIs, right? This time I was addicted to it, so I left it down. Since the thoughts are written as sloppy, if you write it briefly

data_bytes = bytes('Akanechan Kawaii Yatta', 'utf-8')
data_encode_bytes = base64.b64encode(data_bytes)
data_encode_str = data_encode_bytes.decode('utf-8')

These three lines are the result.

Prepared data

In Python, Dict type and Json have the same shape, so after processing with Dict type which is easy to process, it is finally converted to Json.

akane_dict = {
            'kind': 'voiceroid',
            'data': 'Akanechan Kawaii Yatta'
        }

It's packed in various ways. This time I want to encode 'data'` `` with base64 and do` `json.dumps.

Data encoding preparation

Make `'data'` of bytes type to encode.

data_bytes = b'Akanechan Kawaii Yatta'

akane_dict = {
            'kind': 'voiceroid',
            'data': data_bytes
        }

This is no good.

    data = b'Akanechan Kawaii Yatta'
          ^
SyntaxError: bytes can only contain ASCII literal characters.

'Akanechan Kawaii Yatta'Is not ascii, so it's natural (1 loss)

I'm typing characters in UTF-8 for VS Code input, so this seems to work.


data_bytes = bytes('Akanechan Kawaii Yatta', 'utf-8')

akane_dict = {
            'kind': 'voiceroid',
            'data': data_bytes
        }

data encoding

Since base64 is included as standard, you can import it as it is.

import base64

data_bytes = bytes('Akanechan Kawaii Yatta', 'utf-8')
data_encode_bytes = base64.b64encode(data_bytes)

akane_dict = {
    'kind': 'voiceroid',
    'data': data_encode_bytes
}

print(akane_dict) #For confirmation

Convert to Json

We will convert Dict type to Json.

import base64
import json

data_bytes = bytes('Akanechan Kawaii Yatta', 'utf-8')
data_encode_bytes = base64.b64encode(data_bytes)

akane_dict = {
    'kind': 'voiceroid',
    'data': data_encode_bytes
}

akane_json = json.dumps(akane_dict)

The following error occurs.


TypeError: Object of type bytes is not JSON serializable

This can't be converted to Json if it's a bytes type! You will get angry at once, so you need to convert it to str type. (2 losses)

Strategy 1

It's a cast that seems to change the str type! Then it will be like this.

import base64
import json

data_bytes = bytes('Akanechan Kawaii Yatta', 'utf-8')
data_encode_bytes = base64.b64encode(data_bytes)
data_encode_str = str(data_encode_bytes)

akane_dict = {
    'kind': 'voiceroid',
    'data': data_encode_str
}

print(akane_dict)
#Send
akane_json = json.dumps(akane_dict)

#Receive
res = json.loads(akane_json)
print(res)
res_data = base64.b64decode(res['data'])
print(res_data)
binascii.Error: Invalid base64-encoded string: number of data characters (53) cannot be 1 more than a multiple of 4 

I agree. If you cast to str type, it will be like this. Let's take a look at the contents of ``` res``.

{'kind': 'voiceroid', 'data': "b'44Ki44Kr44ON44OB44Oj44Oz44Kr44Ov44Kk44Kk44Ok44OD44K/'"}

It may be possible to send it, but it seems impossible to decode `` `data```.

Strategy 2

Article (1) was found, so refer to it and change the code as follows

import base64
import json

data_bytes = bytes('Akanechan Kawaii Yatta', 'utf-8')
data_encode_bytes = base64.b64encode(data_bytes)
data_encode_str = data_encode_bytes.decode('utf-8')

akane_dict = {
    'kind': 'voiceroid',
    'data': data_encode_str
}

print(akane_dict)
#Send
akane_json = json.dumps(akane_dict)

#Receive
res = json.loads(akane_json)
res_data = base64.b64decode(res['data']).decode('utf-8')
print(res_data)

The strange thing here is that the bytes type is decoded into a character string with utf8. I don't understand why I should do this due to lack of study, but if I do this, json.dumps will work well and the receiving side will not be sharpened with an error. On the receiving side, if it is handled as bytes type, it is not necessary to decode it with utf8. Then display

{'kind': 'voiceroid', 'data': '44Ki44Kr44ON44OB44Oj44Oz44Kr44Ov44Kk44Kk44Ok44OD44K/'}
Akanechan Kawaii Yatta

Yeah, looks good

at the end

It seems a little unorganized, but I wrote an article to find out where I was defeated.

reference

(1) Python: Handle binaries with JSON

Recommended Posts

What I was addicted to with json.dumps in Python base64 encoding
I was addicted to scraping with Selenium (+ Python) in 2020
What I was addicted to Python autorun
What I was addicted to when using Python tornado
What I was addicted to when migrating Processing users to Python
What I was addicted to when introducing ALE to Vim for Python
I was addicted to confusing class variables and instance variables in Python
What I was addicted to when dealing with huge files in a Linux 32bit environment
I was addicted to creating a Python venv environment with VS Code
Use Python from Java with Jython. I was also addicted to it.
The file name was bad in Python and I was addicted to import
What I was addicted to when creating a web application in a windows environment
Base64 encoding images in Python 3
Three things I was addicted to when using Python and MySQL with Docker
I want to work with a robot in python.
pickle To read what was made in 2 series with 3 series
A note I was addicted to when running Python with Visual Studio Code
What I learned in Python
I was able to repeat it in Python: lambda
I was addicted to trying logging.getLogger in Flask 1.1.x
A story that I was addicted to when I made SFTP communication with python
What I was addicted to when combining class inheritance and Joint Table Inheritance in SQLAlchemy
What I did when I was angry to put it in with the enable-shared option
[IOS] GIF animation with Pythonista3. I was addicted to it.
What I did to welcome the Python2 EOL with confidence
[Fixed] I was addicted to alphanumeric judgment of Python strings
What to do with PYTHON release?
I want to debug with Python
I was addicted to multiprocessing + psycopg2
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
I tried to summarize what was output with Qiita with Word cloud
A note I was addicted to when creating a table with SQLAlchemy
I tried to implement PLSA in Python
When I tried to scrape using requests in python, I was addicted to SSLError, so a workaround memo
Try logging in to qiita with Python
I tried to implement permutation in Python
I tried to implement PLSA in Python 2
To set default encoding to utf-8 in python
I was addicted to pip install mysqlclient
I wanted to solve ABC160 with Python
How to work with BigQuery in Python
I want to analyze logs with Python
I was addicted to Flask on dotCloud
I want to play with aws with python
I tried to implement ADALINE in Python
I wanted to solve ABC159 in Python
I tried to implement PPO in Python
What I did with a Python array
To work with timestamp stations in Python
I wanted to solve ABC172 with Python
When I put Django in my home directory, I was addicted to static files with permission errors
What I did to save Python memory
What I was addicted to in Collective Intelligence Chaprter 3. It's not a typo, so I think something is wrong with my code.
What skills do I need to program with the FBX SDK Python?
I want to tweet on Twitter with Python, but I'm addicted to it
A story I was addicted to trying to get a video url with tweepy
Environment maintenance made with Docker (I want to post-process GrADS in Python
[Python] I was addicted to not saving internal variables of lambda expressions
Upload what you got in request to S3 with AWS Lambda Python
[At Coder] What I did to reach the green rank in Python
After all, what should I use to do type comparisons in Python?