json-to-python
TypeScript icon, indicating that this package has built-in type declarations

0.5.2 • Public • Published

json-to-python

Code Style: Prettier Build Status Coverage Status

Simple utility to convert JSON to Python TypedDict or dataclass.

Playground

Try it out: json-to-python Playground

Motivation

As an engineer who appreciates types and works extensively with Python, especially in the realm of AI development, I often encounter the need to communicate with external APIs, like calling a vector database server's API. In cases where an official SDK is not provided, I have to handle the API calls manually. I strongly prefer using types for requests and responses, and I found myself needing an easy way to convert JSON to TypedDict. Not finding a suitable solution, I decided to implement this utility myself.

Usage

This library can be executed in a browser, through the CLI, or used as a library.

Browser Execution

To execute in a browser, please use the json-to-python Playground.

CLI

To execute via CLI, use the following command:

npx json-to-python <json-dir> <output-dir>
npx json-to-python <json-dir> <output-dir> --casing <camel|snake|none> --generate <typeddict|dataclass>

Library

To use this repository as a library, first, install this repository:

npm install json-to-python

Single JSON Conversion

import { generate } from "json-to-python/browser";

const jsonString = JSON.stringify({ name: "Mr X", age: 30, city: "New York" });
const className = "Main";
const config = {
  casing: "camel",
  generate: "typeddict",
};
const python = generate(jsonString, className, config);

Folder Conversion

import { bulkGenerate } from "json-to-python";

const config = {
  casing: "camel",
  generate: "typeddict",
};
const python = bulkGenerate(
  "./path_to_input_dir",
  "./path_to_output_dir",
  config
);

Changelog

Please see ./packages/json-to-python/CHANGELOG.md

Contributing

If you find a bug or have a new feature request, please feel free to open an issue. Pull requests are also welcome.

Readme

Keywords

none

Package Sidebar

Install

npm i json-to-python

Weekly Downloads

20

Version

0.5.2

License

MIT

Unpacked Size

13.2 kB

Total Files

9

Last publish

Collaborators

  • baseballyama