Decode JSON before calling a function

A JSON object arrives as text.

A JSON object arrives as text. json.loads converts it to a Python dictionary. The number 3 then has an integer type and can be multiplied. Without decoding, you still have the serialized text.

Work through the example

The decoded integer is 3. Applying f(x) = 2x gives f(3) = 6.

Run the example as a separate Python file. The inputs are synthetic so you can change them and see exactly which assumption affects the result.

Run the example

import json
arguments = json.loads('{"x": 3}')
print(type(arguments["x"]).__name__)
print(arguments["x"] * 2)

Expected output

int
6

Reference: https://docs.python.org/3/library/json.html

The team guide · My original notes on testing coding agents · Working with Lightbulb Partners