In many AI systems, JSON is a commonly used data format for exchanging information between components, such as APIs, databases, and models. Ensuring that JSON data is both valid and adheres to specific structural requirements is critical for maintaining seamless communication and avoiding system errors.

An AI model might generate JSON for function calls, or external systems might send JSON data for processing. If the JSON is malformed or doesn’t follow the required structure, the workflow could fail, leading to unexpected outcomes or system disruptions.

Future AGI provides solution using which you can validate your JSON data, called “Is JSON” and “JSON Scheme Validation”. It checks if the content is valid JSON, also validates JSON structure against user provided criteria.


1. Is JSON Eval

This evaluation checks whether the input is a valid JSON format. Ensures that the content can be parsed as JSON without syntax errors.

Click here to read the eval definition of Is JSON

a. Using Interface

Input Required

  • Text: The input content to be evaluated.

Output

  • Pass: The content is valid JSON.
  • Fail: The content is not valid JSON (e.g., missing brackets, improper structure).

b. Using SDK

from fi.evals import EvalClient
from fi.testcases import TestCase
from fi.evals.templates import IsJson

test_case = TestCase(
    text='''
    {
        "name": "John Doe",
        "age": 30,
        "city": "New York",
        "hobbies": ["reading", "hiking"]
    }
    '''
)

template = IsJson()

evaluator = EvalClient(
    fi_api_key="your_api_key",
    fi_secret_key="your_secret_key",
    fi_base_url="https://api.futureagi.com"
)

response = evaluator.evaluate(eval_templates=[template], inputs=[test_case])

score = response.eval_results[0].metrics[0].value

2. JSON Schema Validation

This evaluation validates whether a JSON object matches an expected schema or set of user defined criteria. It ensures that the JSON structure aligns with specific requirements, such as required keys, value types, or nested structures.

Click here to read the eval definition of JSON Scheme Validation

a. Using Interface

  • Inputs Required:
    • Actual JSON: The JSON to validate.
    • Expected JSON: The reference JSON schema to validate against.
    • Validations: A list of specific validation criteria (e.g., required fields, allowed value types).
  • Output:
    • Pass: The JSON matches the expected schema.
    • Fail: The JSON does not conform to the required structure.

b. Using SDK

from fi.evals import EvalClient
from fi.testcases import TestCase
from fi.evals.templates import JsonSchemeValidation

test_case = TestCase(
    actual_json={
        "name": "John Doe",
        "age": 30,
        "email": "john@example.com"
    },
    expected_json={
        "type": "object",
        "properties": {
            "name": {"type": "string"},
            "age": {"type": "integer"},
            "email": {"type": "string"}
        },
        "required": ["name", "age", "email"]
    }
)

template = JsonSchemeValidation(
    config={
        "validations": [
            "type_check",
            "required_fields"
        ]
    }
)

evaluator = EvalClient(
    fi_api_key="your_api_key",
    fi_secret_key="your_secret_key",
    fi_base_url="https://api.futureagi.com"
)

response = evaluator.evaluate(eval_templates=[template], inputs=[test_case])

score = response.eval_results[0].metrics[0].value