Editor

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 lesson

This 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

All Python exercises · 50 practice problems · File I/O & Data Formats quiz