# Import a Persona JSON into a Local Fixture

Import a persona JSON into a local test fixture with deterministic Python validation, without an API or third-party dependency.

- Canonical URL: https://www.harmate.com/en/blog/import-a-persona-json-into-a-local-fixture
- Author: Harmate Team
- Published: 2026-11-15
- Updated: 2026-11-15T01:52:15.111881+00:00
- Language: en

## Content

# Import a Persona JSON into a Local Fixture

A JSON profile supplied by a colleague or produced by your own tool can provide context for a test case. Before passing it to a model, check that the file is a JSON object, that its content survives the import, and that the case using it has an explicit identifier. This example shows a local Python integration with no API or third-party dependency.

The wrapper format below is made for this guide. It is not a Harmate schema, import API, or universal persona format. The profile is a fictional example written for this exercise:

```json
{
  "role": "fictional library assistant",
  "work_context": "helps visitors find public information",
  "preferences": [
    "asks one clarifying question when needed",
    "does not claim to reserve books"
  ]
}
```

## Wrap the data without rewriting it

The importer reads the file, rejects an empty object or a root of another type, and places the profile inside a test envelope. The case ID lets you find the associated inputs and outputs later. The profile's keys and values stay as supplied: import does not infer quality or turn preferences into system rules.

A standard JSON parser may accept extensions or silently overwrite a repeated key. To make the input predictable, the demonstration script rejects duplicate keys, non-standard constants such as `NaN`, malformed JSON, and a blank case ID. It then serializes the envelope with sorted keys. A second parse checks that the resulting profile equals the original object.

Run this complete example with `python3 import_fixture.py`, or paste it into a Python file. The sample data is embedded so the example is self-contained:

```python
import json

PROFILE_JSON = '''{"role":"fictional library assistant","work_context":"helps visitors find public information","preferences":["asks one clarifying question when needed","does not claim to reserve books"]}'''

def unique_object(pairs):
    result = {}
    for key, value in pairs:
        if key in result:
            raise ValueError(f"duplicate key: {key}")
        result[key] = value
    return result

def reject_constant(value):
    raise ValueError(f"not standard JSON: {value}")

def import_profile(text, case_id):
    profile = json.loads(text, object_pairs_hook=unique_object,
                         parse_constant=reject_constant)
    if not isinstance(profile, dict) or not profile:
        raise ValueError("profile must be a non-empty object")
    if not isinstance(case_id, str) or not case_id.strip():
        raise ValueError("case_id must be non-empty")
    return {"fixture_format": "tutorial-local-fixture/v1",
            "case_id": case_id, "profile": profile}

def must_reject(text, case_id="library-search-01"):
    try:
        import_profile(text, case_id)
    except (json.JSONDecodeError, ValueError):
        return
    raise AssertionError("invalid input was accepted")

fixture = import_profile(PROFILE_JSON, "library-search-01")
encoded = json.dumps(fixture, ensure_ascii=False, sort_keys=True, indent=2)
decoded = json.loads(encoded)
assert decoded["profile"] == json.loads(PROFILE_JSON)
assert encoded == json.dumps(decoded, ensure_ascii=False, sort_keys=True, indent=2)
for bad in ("[]", "{}", '{"role":"a","role":"b"}',
            '{"role":NaN}', "{broken"):
    must_reject(bad)
must_reject(PROFILE_JSON, "  ")
print("PASS imported profile preserved")
print("PASS sorted serialization is deterministic")
print("PASS rejected six invalid inputs")
print("PASS no network, model, or API call")
```

Expected output:

```text
PASS imported profile preserved
PASS sorted serialization is deterministic
PASS rejected six invalid inputs
PASS no network, model, or API call
```

The full code above is the code executed for this example. It validates structure and preservation only; it does not judge the profile or evaluate a model.

## Keep import separate from evaluation

Validation covers the file's structure and preservation, not the profile's real-world accuracy or the quality of a generated answer. A separate test can pair the imported profile with a user message, checkable expectations, and an output to review. Save the profile, case, and response with their revisions so you know exactly what was replayed.

Before using real data, remove unnecessary identifiers and follow your team's storage rules. Keep secrets out of fixtures. A local example does not show that a provider supports a format or that a system is production-ready. It shows only that your own importer accepts the intended data and makes rejected inputs visible. For a single-case replay example, see [Build an AI Agent Regression Test with Versioned Persona Data](https://www.harmate.com/en/blog/build-an-ai-agent-regression-test-with-versioned-persona-data).

The [public persona example](https://www.harmate.com/en/pricing/api#personae-example-title) shows one profile you can inspect. The script above uses a separate fictional profile and its own format.