Menu
Coddy logo textTech

Handling JSON Data Types

Lesson 8 of 9 in Coddy's Python JSON course.

After parsing JSON data using json.loads(), you'll need to work with various JSON data types in Python. Understanding how these types are represented in Python is crucial for effective data manipulation. Let's explore the main JSON data types and their Python equivalents:

1. JSON Objects (Python Dictionaries)

JSON objects are converted to Python dictionaries:


import json

json_string = '{"name": "John", "age": 30}'
python_dict = json.loads(json_string)

print(type(python_dict))  # Output: <class 'dict'>
print(python_dict["name"])  # Output: John

2. JSON Arrays (Python Lists)

JSON arrays become Python lists:


json_array = '[1, 2, 3, 4, 5]'
python_list = json.loads(json_array)

print(type(python_list))  # Output: <class 'list'>
print(python_list[2])  # Output: 3

3. JSON Strings (Python Strings)

JSON strings are represented as Python strings:


json_string = '"Hello, World!"'
python_string = json.loads(json_string)

print(type(python_string))  # Output: <class 'str'>
print(python_string)  # Output: Hello, World!

4. JSON Numbers (Python int or float)

JSON numbers become Python integers or floats:


json_integer = '42'
json_float = '3.14'

python_integer = json.loads(json_integer)
python_float = json.loads(json_float)

print(type(python_integer))  # Output: <class 'int'>
print(type(python_float))  # Output: <class 'float'>

5. JSON Booleans (Python bool)

JSON true and false are converted to Python True and False:


json_bool = 'true'
python_bool = json.loads(json_bool)

print(type(python_bool))  # Output: <class 'bool'>
print(python_bool)  # Output: True

6. JSON null (Python None)

JSON null becomes Python None:


json_null = 'null'
python_none = json.loads(json_null)

print(type(python_none))  # Output: <class 'NoneType'>
print(python_none)  # Output: None

By understanding these type conversions, you can effectively work with parsed JSON data in your Python programs, accessing and manipulating the data as native Python objects.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

Create a function that processes a JSON string containing a mixed array of different data types. The function should:

  1. Parse the JSON string using json.loads().
  2. Iterate through the parsed array and perform the following operations:
    • For strings: convert to uppercase
    • For numbers (int or float): multiply by 2
    • For booleans: invert the value
    • For null: replace with the string "null_value"
    • For nested arrays: sum all numeric values (ignore non-numeric)
    • For nested objects: count the number of key-value pairs
  3. Return a new list with the processed values.

The following input will be provided:


["hello", 42, true, null, [1, "two", 3], {"a": 1, "b": 2}]

Print the resulting list as a string, with elements separated by commas.

Try it yourself

import json

# Read input
json_string = input()

# Parse JSON string
data = json.loads(json_string)

def process_json_array(arr):
    # TODO: Write your code here
    # Process the array according to the given rules
    # Return the processed list
    pass

# Process the data
result = process_json_array(data)

# Output the result
print(','.join(map(str, result)))

All lessons in Python JSON