Bernard Aybouts - Blog - Miltonmarketing.com

Approx. read time: 2.6 min.

Post: Python Serialization (JSON)

Python Serialization (JSON) Python provides built-in JSON libraries to encode and decode JSON.

In Python 2.5, the simplejson module is used, whereas in Python 2.7, the json module is used. We will be using json for Python versions 2.7+.

In order to use the json module, it must first be imported:

[python] import json[/python]

There are two basic formats for JSON data. Either in a string or the object datastructure. The object datastructure, in Python, consists of lists and dictionaries nested inside each other. The object datastructure allows one to use python methods (for lists and dictionaries) to add, list, search and remove elements from the datastructure. The String format is mainly used to pass the data into another program or load into a datastructure.

To load JSON back to a data structure, use the “loads” method. This method takes a string and turns it back into the json object datastructure:

[python 1=”json” 2=”print(json.loads(json_string))” 3=”[/python” language=”import”]

To encode a data structure to <strong>JSON</strong>, use the <strong>"dumps"</strong> method. This method takes an object and returns a String:

[python] import json
json_string = json.dumps([1, 2, 3, "a", "b", "c"])
print(json_string)[/python]

Trinket.io on-line Python compiler

Python supports a Python proprietary data serialization method called pickle (and a faster alternative called cPickle).

You can use it exactly the same way.

[python] import pickle
pickled_string = pickle.dumps([1, 2, 3, "a", "b", "c"])
print(pickle.loads(pickled_string))[/python]

Trinket.io on-line Python compiler

The aim of this exercise is to print out the JSON string with the key-value pair “Me” : 800 added to it.

[python] import json

# This is the answer code. Try the exercise
# below on your own, using what you have
# learned thus far before checking answer.
def add_employee(salaries_json, name, salary):
salaries = json.loads(salaries_json)
salaries[name] = salary

return json.dumps(salaries)

# test code
salaries = ‘{"Alfred" : 300, "Jane" : 400 }’
new_salaries = add_employee(salaries, "Me", 800)
decoded_salaries = json.loads(new_salaries)
print(decoded_salaries["Alfred"])
print(decoded_salaries["Jane"])
print(decoded_salaries["Me"])[/python]

Trinket.io on-line Python compiler


Related Posts:

Python Basic String Operations

Learn about Python Sets (lists)

How to make a Go-Back Input button with inline JavaScript

Introduction to JavaScript – Variables: Review

Why do most sites use cookies?

Python String Formatting

Introduction to JavaScript – Variables: String Interpolation

Introduction to JavaScript – Review Types and Operators

Object-Oriented Programming (OOP)

Learn List Comprehensions in Python Programming

Introduction to JavaScript – Variables: Undefined

Introduction to JavaScript – Control Flow

Learn Python Variables and Types

Introduction to JavaScript – Control Flow: if/else Statements

Introduction to JavaScript – Control Flow: True and False values

CODING WITH CSS: The style attribute

Learn Partial functions Python programming

Introduction to Python Programming Language

Introduction to JavaScript – Variables: String Interpolation II

Learn about Numpy Arrays in Python programming

Learn Multiple Function Arguments in Python

The Longevity Blueprint: AI-Powered Health Optimization

Current step:1AI-Human Medical Analyzer: Smarter, Personalized Health
2AI-Human Medical Analyzer: Smarter, Personalized Health

> SYS.HEALTH: AI-Human Medical Analyzer_

// Revolutionize Your Diagnostics

Experience the perfect blend of cutting-edge AI precision and expert human care. Our revolutionary analyzer turns your raw health data into personalized, actionable insights tailored just for you.

> INITIALIZING_BIOMETRIC_SCAN...

[+] DATA_INPUT

Securely upload complex health parameters, including lab bloodwork and comprehensive medical history.

[+] PROCESSING

Advanced algorithmic parsing combined with human-level oversight ensures hyper-accurate data interpretation.

[+] OUTPUT_MATRIX

Receive smarter, faster, and truly personalized care strategies to take immediate charge of your health journey.

A name/nickname is required to continue.

> TRANSLATION_MATRIX_ACTIVE...
[ LANG_EN ]
Knowledge Heals, Prevention Protects
[ LANG_HI ]
ज्ञान ठीक करता है, रोकथाम सुरक्षा करती है
[ LANG_ZH ]
知识治愈,预防保护
[ LANG_JA ]
知識は癒し、予防は守る
[ LANG_HE ]
הידע מרפא, המניעה מגנה
[ LANG_AR ]
المعرفة تُشفي، والوقاية تحمي
[ LANG_FR ]
La connaissance guérit, la prévention protège

> SYS.AUTH: Data Processing Consent_

[ AWAITING_AUTHORIZATION ] By providing consent, you allow us to process your uploaded data through our proprietary AI-Human analysis system.

  • [+] SECURE_REVIEW: This ensures your information is carefully reviewed using advanced AI technology and certified professional oversight to deliver personalized health insights.
  • [+] PRIVACY_LOCK: Your privacy is our strict priority. Your data will only be used for this specific diagnostic purpose.

> SYS.UPLOAD: Share Medical Records [OPTIONAL]_

[ USER_CONTROL_ACTIVE ] Uploading your medical records during registration is entirely optional. You can choose to bypass this step and provide data later if it suits your timeline.

You dictate the data flow: share as much or as little as you’re comfortable with, and let us guide you toward better health.

[+] FORMAT_SUPPORT

We accept all file formats, including photos, PDFs, text documents, and raw official medical data.

[+] DATA_YIELD

Increased inputs correlate with higher precision. The more info you share, the better we tailor your personalized insights.

> NEXT_STEPS: Post-Registration Protocol_

Once your registration is complete, a human specialist from our team will personally reach out to you within 3-10 business days. We will discuss your health journey and map out exactly how we can support you.

About the Author: Bernard Aybout (Virii8)

Avatar Of Bernard Aybout (Virii8)
I am a dedicated technology enthusiast with over 45 years of life experience, passionate about computers, AI, emerging technologies, and their real-world impact. As the founder of my personal blog, MiltonMarketing.com, I explore how AI, health tech, engineering, finance, and other advanced fields leverage innovation—not as a replacement for human expertise, but as a tool to enhance it. My focus is on bridging the gap between cutting-edge technology and practical applications, ensuring ethical, responsible, and transformative use across industries. MiltonMarketing.com is more than just a tech blog—it's a growing platform for expert insights. We welcome qualified writers and industry professionals from IT, AI, healthcare, engineering, HVAC, automotive, finance, and beyond to contribute their knowledge. If you have expertise to share in how AI and technology shape industries while complementing human skills, join us in driving meaningful conversations about the future of innovation. 🚀