3Accessing Claude with the API

Structured data exercise

2 min read304 words

Time: 10–15 minutes

Get hands-on with the prefill + stop_sequence pattern by extracting a clean, parseable JSON object from Claude and validating it with json.loads().

Step 1Write the Prompt

Pick a realistic target shape. A good candidate: a user profile with name, role, years_experience, and a skills array of three to five strings.

Create a fresh messages list and append a user message that asks Claude to generate that object. Be explicit about the field names you want — vague prompts produce inconsistent keys.

messages = []
add_user_message(
    messages,
    "Generate a JSON object for a fictional senior engineer with fields: "
    "name, role, years_experience, and skills (an array of 3-5 strings)."
)
1 / 3

You can now reliably extract clean, machine-readable structured data from Claude for use in any application.