Pythonic JSON Data Handling

PythonPythonBeginner
Practice Now

Introduction

JSON (JavaScript Object Notation) is a popular data format for storing data in a serialized and human-readable form. It is commonly used for transmitting data between a server and a web application and is also a great choice for storing data in a NoSQL database. In Python, we can use the json module to work with JSON data.

Achievements

  • Read and write JSON files
  • Serialize and deserialize JSON data

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/FileHandlingGroup -.-> python/file_opening_closing("`Opening and Closing Files`") python/FileHandlingGroup -.-> python/file_reading_writing("`Reading and Writing Files`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/PythonStandardLibraryGroup -.-> python/data_serialization("`Data Serialization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-85{{"`Pythonic JSON Data Handling`"}} python/with_statement -.-> lab-85{{"`Pythonic JSON Data Handling`"}} python/variables_data_types -.-> lab-85{{"`Pythonic JSON Data Handling`"}} python/for_loops -.-> lab-85{{"`Pythonic JSON Data Handling`"}} python/lists -.-> lab-85{{"`Pythonic JSON Data Handling`"}} python/tuples -.-> lab-85{{"`Pythonic JSON Data Handling`"}} python/dictionaries -.-> lab-85{{"`Pythonic JSON Data Handling`"}} python/importing_modules -.-> lab-85{{"`Pythonic JSON Data Handling`"}} python/standard_libraries -.-> lab-85{{"`Pythonic JSON Data Handling`"}} python/file_opening_closing -.-> lab-85{{"`Pythonic JSON Data Handling`"}} python/file_reading_writing -.-> lab-85{{"`Pythonic JSON Data Handling`"}} python/data_collections -.-> lab-85{{"`Pythonic JSON Data Handling`"}} python/data_serialization -.-> lab-85{{"`Pythonic JSON Data Handling`"}} python/build_in_functions -.-> lab-85{{"`Pythonic JSON Data Handling`"}} end

JSON Introduction

JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data interchange language.

The basic structure of JSON consists of objects, which are collections of key-value pairs, and arrays, which are ordered lists of values.

Here is an example of a JSON object:

{
  "name": "Alice",
  "age": 26,
  "city": "New York"
}

This JSON object has three key-value pairs:

  • "name" : "Alice"
  • "age" : 26
  • "city" : "New York"

Here is an example of a JSON array:

["apple", "banana", "cherry"]

This JSON array has three values: "apple", "banana", and "cherry".

A JSON object can contain multiple key-value pairs and arrays, and a JSON array can contain multiple objects and arrays. Here is an example of a more complex JSON structure:

{
  "employees": [
    { "firstName": "John", "lastName": "Doe" },
    { "firstName": "Anna", "lastName": "Smith" },
    { "firstName": "Peter", "lastName": "Jones" }
  ],
  "owners": [
    { "name": "Alice", "age": 30 },
    { "name": "Bob", "age": 35 }
  ],
  "office": {
    "name": "Acme Inc.",
    "location": "New York"
  }
}

This JSON object has three key-value pairs:

  • "employees" : an array of employee objects
  • "owners" : an array of owner objects
  • "office" : an office object

Json Module

To parse a JSON object in Python, we can use the json module. The json module is part of the Python standard library, so we don't need to install it separately.

Open up a new Python interpreter session and type the following code:

python3

Here is an example of how to parse the JSON object from the previous example:

import json

json_strings = '{"employees": [{ "firstName": "John", "lastName": "Doe"}, ], "owners": [{ "name": "Alice", "age": 30 }, { "name": "Bob", "age": 35 }], "office": {"name": "Acme Inc."}}'

## Parse the JSON object
data = json.loads(json_strings)

## Access the data
employees = data['employees']
owners = data['owners']
office = data['office']

## Print the data
print(employees)
print(owners)
print(office)

This will output the following:

[{'firstName': 'John', 'lastName': 'Doe'}]
[{'name': 'Alice', 'age': 30}, { "name": "Bob", "age": 35 }]
{'name': 'Acme Inc.'}

You can then access the data in the JSON object using keys and indexes, just like you would with a Python dictionary or list. For example, to access the first employee's first name, you can use the following code:

first_employee = employees[0]
first_name = first_employee['firstName']
print(first_name)

This will output the following:

John

You can also loop through the arrays and access the data for each element. For example, to print the name and age of each owner, you can use the following code:

for owner in owners:
  name = owner['name']
  age = owner['age']
  print(f'{name} is {age} years old.')

This will output the following:

Alice is 30 years old.
Bob is 35 years old.

Reading JSON Files

Let's start by reading a JSON file. Suppose we have a file employees.json with the following contents:

{
  "employees": [
    {
      "firstName": "John",
      "lastName": "Doe"
    },
    {
      "firstName": "Anna",
      "lastName": "Smith"
    },
    {
      "firstName": "Peter",
      "lastName": "Jones"
    }
  ]
}

employees.json has been existing in the /home/labex/project.

To read this file in Python, we can use the following code:

import json

## Open the file
with open('/home/labex/project/employees.json', 'r') as file:
  ## Load the contents of the file into a variable
  data = json.load(file)

## Print the contents of the file
print(data)

This will output the following:

{'employees': [{'firstName': 'John', 'lastName': 'Doe'}, {'firstName': 'Anna', 'lastName': 'Smith'}, {'firstName': 'Peter', 'lastName': 'Jones'}]}

We can access the data in the file by using the keys of the JSON object, just like we would with a Python dictionary. For example, to access the list of employees, we can use the employees key:

employees = data['employees']
print(employees)

This will output the following:

[{'firstName': 'John', 'lastName': 'Doe'}, {'firstName': 'Anna', 'lastName': 'Smith'}, {'firstName': 'Peter', 'lastName': 'Jones'}]

We can also loop through the list of employees and print their names:

for employee in employees:
  print(employee['firstName'], employee['lastName'])

This will output the following:

John Doe
Anna Smith
Peter Jones

Writing JSON Files

Now that we know how to read a JSON file, let's learn how to write to one. We can use the json.dump() function to write data to a JSON file.

Suppose we have a list of dictionaries containing information about students. We can write this data to a file called students.json like this:

import json

## A list of dictionaries
students = [
  { "name": "Alice", "age": 26, "major": "Computer Science" },
  { "name": "Bob", "age": 27, "major": "Economics" },
  { "name": "Charlie", "age": 25, "major": "Physics" }
]

## Open the file
with open('students.json', 'w') as file:
  ## Write the data to the file
  json.dump(students, file)

Now let's verify that the data was written correctly:

with open('students.json', 'r') as file:
  data = json.load(file)
  print(data)

This will output the following:

[
  { "name": "Alice", "age": 26, "major": "Computer Science" },
  { "name": "Bob", "age": 27, "major": "Economics" },
  { "name": "Charlie", "age": 25, "major": "Physics" }
]

We can also use the json.dumps() function to convert a Python object to a JSON string, which we can then save to a file or send over the network. Here's an example:

## Convert the list of dictionaries to a JSON string
json_string = json.dumps(students)
print(json_string)

## Save the string to a file
with open('students.json', 'w') as file:
  file.write(json_string)

## Now let's verify that the data was written correctly
with open('students.json', 'r') as file:
  data = json.load(file)
  print(data)

This will have the same output as the previous example.

Python Object and JSON String

A Python object is a collection of data in the form of lists, dictionaries, etc. that is stored in memory. A JSON string, on the other hand, is a serialized representation of a JSON object. It is a string of characters that represents the data in the JSON object, but it is not stored in memory as an actual object.

Here is an example of how to use the read() function to read the contents of students.json:

with open('students.json', 'r') as file:
  json_string = file.read()
  print(json_string)

This will output the following:

[{"name": "Alice", "age": 26, "major": "Computer Science"}, {"name": "Bob", "age": 27, "major": "Economics"}, {"name": "Charlie", "age": 25, "major": "Physics"}]

When you try to get the first element of the json_string, you will get the first character of the string:

print(json_string[0])
[

This is because json_string is a string, not a list (Python Object). To convert it to a list, we can use the json.loads() function:

import json

## Convert the JSON string to a Python object
python_object = json.loads(json_string)
print(python_object)

The output is similar to the above, with the difference between single and double quotes.

[{'name': 'Alice', 'age': 26, 'major': 'Computer Science'}, {'name': 'Bob', 'age': 27, 'major': 'Economics'}, {'name': 'Charlie', 'age': 25, 'major': 'Physics'}]

Now we can access the first element of the list:

print(python_object[0])
{ "name": "Alice", "age": 26, "major": "Computer Science" }

Beginners often get Python Object and JSON String confused, so pay special attention.

Summary

Congratulations! You've completed the lab on JSON files. You've learned how to read and write JSON files in Python. You've also learned how to work with JSON data in Python.

Other Python Tutorials you may like