Python Data Type Conversions: str ↔ list ↔ JSON and Common Transformations
This article explains how to convert between strings, lists, dictionaries, and JSON in Python using ast.literal_eval, json.loads, and json.dumps, and demonstrates practical applications such as API response handling in automation testing.
1. str to list
1.1 Using ast.literal_eval
If the string represents a valid Python list, you can safely parse it with import ast str_list = "[1, 2, 3, 'hello', {'key': 'value'}]" converted_list = ast.literal_eval(str_list) print(converted_list) # Output: [1, 2, 3, 'hello', {'key': 'value'}]
1.2 Using json.loads (for JSON‑formatted strings)
When the string follows JSON syntax, import json json_str = '[1, 2, 3, "hello", {"key": "value"}]' converted_list = json.loads(json_str) print(converted_list) # Output: [1, 2, 3, "hello", {"key": "value"}]
2. str to json
2.1 Using json.loads
Parse a JSON‑style string into a Python object with import json json_str = '{"name": "Alice", "age": 25, "city": "New York"}' converted_json = json.loads(json_str) print(converted_json) # Output: {'name': 'Alice', 'age': 25, 'city': 'New York'}
3. json to list
3.1 Using json.loads and extracting a list
Load a JSON array and treat it as a Python list: import json json_str = '[{"name": "Alice", "age": 25}, {"name": "Bob", "age": 30}]' json_data = json.loads(json_str) converted_list = json_data print(converted_list) # Output: [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}]
4. list to json
4.1 Using json.dumps
Serialize a Python list to a JSON string: import json my_list = [1, 2, 3, "hello", {"key": "value"}] json_str = json.dumps(my_list) print(json_str) # Output: [1, 2, 3, "hello", {"key": "value"}]
5. Other common conversions
5.1 dict to JSON string
import json my_dict = {"name": "Alice", "age": 25, "city": "New York"} json_str = json.dumps(my_dict) print(json_str) # Output: {"name": "Alice", "age": 25, "city": "New York"}
5.2 JSON string to dict
import json json_str = '{"name": "Alice", "age": 25, "city": "New York"}' converted_dict = json.loads(json_str) print(converted_dict) # Output: {'name': 'Alice', 'age': 25, 'city': 'New York'}
6. Example: Application in API automation
6.1 Receive response and convert to dict
import requests import json response = requests.get('https://api.example.com/data') json_data = response.json() print(json_data) # Output: JSON object
6.2 Process JSON data
import json json_str = '[{"name": "Alice", "age": 25}, {"name": "Bob", "age": 30}]' json_data = json.loads(json_str) names = [person['name'] for person in json_data] print(names) # Output: ['Alice', 'Bob']
7. Summary
We covered common Python data‑type conversion methods useful in interface automation testing: str→list (ast.literal_eval or json.loads), str→json (json.loads), json→list (json.loads with extraction), list→json (json.dumps), plus dictionary↔JSON string conversions.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.