jsonanything
v1.1.1
Published
A JavaScript SDK for interacting with the JSONAnything API
Downloads
15
Readme
JSONAnything SDK
JSONAnything SDK is a powerful tool for parsing unstructured text into structured JSON data using predefined schemas. This SDK provides an easy-to-use interface for interacting with the JSONAnything API.
For more information about JSONAnything and to sign up for an API key, visit our website: https://jsonanything.com
Installation
You can install the JSONAnything SDK using npm or pnpm:
npm install jsonanything
# or
pnpm add jsonanything
Usage
Here's a basic example of how to use the JSONAnything SDK:
import JSONAnything from "jsonanything";
import { z } from "zod";
// Initialize the SDK
const jsonAnything = new JSONAnything({
API_KEY: "your_api_key_here", // You can also set this as an environment variable JSONANYTHING_API_KEY
});
// Define your schema using Zod
const schema = z.object({
personalInfo: z.object({
name: z.string(),
age: z.number().int().min(0).max(120),
email: z.string().email(),
}),
professionalInfo: z.object({
occupation: z.string(),
yearsOfExperience: z.number().int().min(0),
skills: z.array(z.string()),
}),
});
// Your unstructured text
const text = `
John Doe is a 35-year-old software engineer with 10 years of experience.
He can be reached at [email protected]. John is skilled in JavaScript,
Python, and Machine Learning.
`;
// Parse the text
jsonAnything
.parseJson(text, schema)
.then((result) => console.log(result))
.catch((error) => console.error("Error:", error));
API Reference
Constructor
new JSONAnything(config?: JSONAnythingConfig)
config.API_KEY
(optional): Your JSONAnything API key. If not provided, the SDK will look for an environment variable namedJSONANYTHING_API_KEY
.config.BASE_URL
(optional): The base URL for the JSONAnything API. Defaults to 'https://jsonanything.com/api'.
Methods
parseJson
parseJson(text: string, zodSchema: z.ZodType<any>, file: string = ""): Promise<any>
text
: The unstructured text to parse.zodSchema
: A Zod schema defining the structure of the expected JSON output.file
(optional): A file reference, if applicable.
Returns a Promise that resolves to the parsed JSON data.
Error Handling
The SDK throws errors for invalid API keys, network issues, or API response errors. Always wrap your API calls in try-catch blocks or use .catch()
on the returned promises to handle potential errors gracefully.
TypeScript Support
This SDK is written in TypeScript and provides type definitions out of the box. This ensures excellent IDE support and type safety when using the SDK in TypeScript projects.
Contributing
We welcome contributions to the JSONAnything SDK! Please see our Contributing Guide for more details.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Additional Resources
For more information, documentation, and support, visit our website: https://jsonanything.com