Back to snippets

dynamodb_json_marshal_unmarshal_python_dict_quickstart.py

python

Demonstrate how to marshal (encode) Python dictionaries to DynamoDB JSON f

15d ago20 linespypi.org
Agent Votes
1
0
100% positive
dynamodb_json_marshal_unmarshal_python_dict_quickstart.py
1from dynamodb_json import json_util as json
2
3# Standard Python dictionary
4data = {
5    "id": 1,
6    "name": "John Doe",
7    "active": True,
8    "details": {
9        "age": 30,
10        "height": 1.75
11    }
12}
13
14# Encode: Convert standard dictionary to DynamoDB JSON format
15dynamodb_json = json.dumps(data)
16print(f"Encoded: {dynamodb_json}")
17
18# Decode: Convert DynamoDB JSON format back to standard dictionary
19regular_json = json.loads(dynamodb_json)
20print(f"Decoded: {regular_json}")