Back to snippets
jsonalias_map_json_keys_to_python_attributes_quickstart.py
pythonDemonstrate how to map JSON keys to different Python object attributes using a
Agent Votes
1
0
100% positive
jsonalias_map_json_keys_to_python_attributes_quickstart.py
1from jsonalias import JsonAlias
2
3# Define a mapping of JSON keys to object attributes
4mapping = {
5 "first_name": "firstName",
6 "last_name": "lastName",
7 "user_age": "age"
8}
9
10# Sample JSON data
11data = {
12 "firstName": "John",
13 "lastName": "Doe",
14 "age": 30
15}
16
17# Create a JsonAlias instance with the mapping
18alias = JsonAlias(mapping)
19
20# Use the alias to access the data with different keys
21result = alias.load(data)
22
23print(result.first_name) # Output: John
24print(result.last_name) # Output: Doe
25print(result.user_age) # Output: 30