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

json-to-py

v1.0.1

Published

Generate Python TypedDict classes from JSON data.

Downloads

5

Readme

json-to-py

Generate Python TypedDict classes from JSON data.

Installation

To install json-to-py globally, use:

npm install -g json-to-py

Usage

You can use json-to-py via the command line. The primary command is start, which generates Python classes from a specified JSON file.

Command Line Options

  • --json or -j: Path to the JSON file (required)
  • --name or -n: Name of the root class (default: "RootInterface")
  • --output or -o: Path to the output Python file (default: "output.py")

Example

json-to-py start --json input.json --name MyClass --output my_classes.py

This command will read the JSON data from input.json, generate Python TypedDict classes with the root class named MyClass, and write the output to my_classes.py.

Example JSON

Here is an example JSON input:

{
  "data": [{
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "JSON:API paints my bikeshed!",
      "body": "The shortest article. Ever.",
      "created": "2015-05-22T14:56:29.000Z",
      "updated": "2015-05-22T14:56:28.000Z"
    },
    "relationships": {
      "author": {
        "data": {"id": "42", "type": "people"}
      }
    }
  }],
  "included": [
    {
      "type": "people",
      "id": "42",
      "attributes": {
        "name": "John",
        "age": 80,
        "gender": "male"
      }
    }
  ]
}

Example Generated Python

The above JSON would generate the following Python classes:

from typing import TypedDict, List, Any

class Attributes(TypedDict):
    title: str
    body: str
    created: str
    updated: str

class AuthorData(TypedDict):
    id: str
    type: str

class Author(TypedDict):
    data: AuthorData

class Relationships(TypedDict):
    author: Author

class Data(TypedDict):
    type: str
    id: str
    attributes: Attributes
    relationships: Relationships

class IncludedAttributes(TypedDict):
    name: str
    age: int
    gender: str

class Included(TypedDict):
    type: str
    id: str
    attributes: IncludedAttributes

class RootInterface(TypedDict):
    data: List[Data]
    included: List[Included]

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Malik Kurosaki

This README.md provides a comprehensive guide for users on how to install and use the json-to-py tool, along with an example JSON input and the corresponding generated Python output.