How to Encode Json to Base64 in Python: 4 Steps Only

How to Encode Json to Base64 in Python

Base64 is used to encode the data to text from the binary data. It is mostly used for encoding images, audio, or binary file contents into ASCII text. If you want to encode JSON data to Base64 then this post is for you. In this entire post you will know all the steps required to encode Json to Bast64 in python.

Steps to Encode Json to Base64 in Python

Let’s know all the step that you will use for encoding JSON to base64 in python. Follow all the steps carefully for more understanding.

Step 1: Import the required library

The first and most important step is to import all the required libraries that will be used in this example. I will use the json module and base64 only. So let’si mport it using the import statement.

import json
import base64

Step 2: Create a JSON data

The next step is to create JSON data that will be used to encode it to base64. It is not compulsory to create it. You can use your own JSON data. Use the below lines of code to create JSON data.

json_data = {
    "website": "Data Science Learner",
    "age": "7",
    "topics": ["Data Science","Machine Learning","AI"]
}

Step 3: Convert the JSON data to string

Before converting the JSON data to base64 you have to first convert the JSON data to string using the json.dumps() function. Use the below line of code to convert the Json data to string.

json_string = json.dumps(json_data)

Step 4: Encode the string to base64

Now after the conversion you will convert the string to the base64 using the b64encode() function. You will just pass the Json string and encode the string using the utf-8 encoder.

Add  the below lines of code to achieve the above step.

base64_encoded = base64.b64encode(json_string.encode('utf-8'))

Thats all you have to do Encode Json to Base64 in Python. If you want to decode the data then you have to first decode the encoded data and use the print function.

base64_encoded_string = base64_encoded.decode('utf-8')

Below is the Full code for the above example.

import json
import base64

json_data = {
    "website": "Data Science Learner",
    "age": "7",
    "topics": ["Data Science","Machine Learning","AI"] }

json_string = json.dumps(json_data)

base64_encoded = base64.b64encode(json_string.encode('utf-8'))
base64_encoded_string = base64_encoded.decode('utf-8')

print("Encoded JSON data in Base64:", base64_encoded_string)

Output

encoded json to base64
encoded json to base64

Conclusion

It is best to encode the JSON data for text representation, data transmission, and data storage. The above step will allow you to encode any JSON data to base64.  You have to first convert the JSON data to string and then encode the string to base64 using the b64encode function.

I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner