npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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 named JSONANYTHING_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