This API provides a service for converting unstructured text data into structured JSON format based on a provided schema. It uses Google Generative AI to generate structured JSON output from text input, adhering to the given schema.
- CORS support: Allows cross-origin requests.
- Zod schema validation: Converts JSON schema to Zod schema for input validation.
- Retry mechanism: Implements retry logic to handle transient errors.
- Google Generative AI integration: Leverages Google Generative AI for content generation.
- Clone the repository:
git clone https://github.com/your-username/json-formatter-api.git
cd json-formatter-api- Install dependencies:
npm install- Set up environment variables:
Create a .env file and add your Google API key:
GOOGLE_API_KEY=your-google-api-key
- Start the server:
npm start- Make a POST request to the API with the following payload:
{
"input": "Your unstructured text here.",
"format": {
"your": {"type": "string"},
"desired": {"type": "string"},
"json": {"type": "string"},
"format": {"type": "string"}
}
}- Example cURL request:
curl -X POST http://localhost:3000/ \
-H "Content-Type: application/json" \
-d '{
"input": "Your unstructured text here.",
"format": {
"your": {"type": "string"},
"desired": {"type": "string"},
"json": {"type": "string"},
"format": {"type": "string"}
}
}'hono: Web framework for building APIs.cors: Middleware for enabling CORS.zod: TypeScript-first schema declaration and validation library.@google/generative-ai: Google Generative AI library.
Accepts a JSON payload with input and format fields, processes the input using Google Generative AI, and returns a structured JSON response based on the provided format.
determineSchemaType(schema: any): string: Determines the type of a JSON schema.jsonSchemaToZod(schema: any): ZodTypeAny: Converts JSON schema to Zod schema.RetryablePromise<T>: A class that extendsPromisewith retry logic.
Here’s an example of how the API processes input:
- Input:
{
"input": "My name is John and I am 25 years old.",
"format": {
"name": {"type": "string"},
"age": {"type": "number"}
}
}- Output:
{
"name": "John",
"age": 25
}