Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to convert Base64 to a JSON string in Python

1 Answer

0 votes
import base64
import json

def base64_to_pretty_json(base64_string: str) -> str:
    """
    Decode a Base64 string, parse it as JSON, and return a pretty-printed JSON string.
    """
    try:
        # Decode Base64 to plain text
        decoded_bytes = base64.b64decode(base64_string)
        decoded_string = decoded_bytes.decode("utf-8")
 
        # Parse plain text to JSON
        json_object = json.loads(decoded_string)

        # Convert JSON object to pretty JSON string
        return json.dumps(json_object, indent = 2)
    except Exception as e:
        return f"Error converting Base64 to JSON: {e}"


if __name__ == "__main__":
    base64_string = "ewogICJ1c2VybmFtZSI6ICJPa2FiZSIsCiAgImFnZSI6IDM3Cn0="
    
    result = base64_to_pretty_json(base64_string)
    
    print("JSON String:")
    print(result)




'''
run:

JSON String:
{
  "username": "Okabe",
  "age": 37
}

'''

 



answered Dec 8, 2025 by avibootz
...