Refactor String Concatenation to json.dumps
Intermediate · Refactor · from Python JSON: Parse, Create, and Manipulate JSON Data
From the lesson: Python JSON: Parse, Create, and Manipulate JSON Data
Imagine a universal language that every app, website, and database can understand. That's JSON (JavaScript Object Notation). It's the most popular format for sending data between computers.
Read the lessonThis code manually builds a JSON string using string concatenation, which is error-prone. Refactor it to use json.dumps() instead. The output should be identical.
Expected output:
{"name": "Bob", "scores": [90, 85, 92]}# Refactor this to use json.dumps()
result = '{"name": "Bob", "scores": [90, 85, 92]}'
print(result)
More JSON exercises
- Parse a JSON StringBeginner
- Predict the JSON OutputBeginner
- Create Pretty JSONIntermediate
- Fix the JSON Parsing BugIntermediate
- Extract Data from Nested API JSONAdvanced
All Python exercises · 50 practice problems · File I/O & Data Formats quiz