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:

import json

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:

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

import json
json_string = json.dumps([1, 2, 3, "a", "b", "c"])
print(json_string) 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.

import pickle
pickled_string = pickle.dumps([1, 2, 3, "a", "b", "c"])
print(pickle.loads(pickled_string)) 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.

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["Me"]) 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