Skip to main content

Parse json to python dataclass

Installion

pip3 install pyjson2dataclass

Usage

json2dataclass

usage: json2dataclass [-h] [-d SAVE_DIR] [-i [INPUT ...]] [-p] [-f]

options:
  -h, --help            show this help message and exit
  -d SAVE_DIR, --save-dir SAVE_DIR
                        specify the directory parsed dataclass file save to
  -i [INPUT ...], --input [INPUT ...]
                        specify the input json file path
  -p, --pascal          use pascalName for parsed dataclass filename
  -f, --force           enable overwrite exists dataclass
json2dataclass -i <json file1> <json file2> ... -d dataclass

What above did is parse json file passed by -i to dataclass python file and save them to directory 'dataclass'

json2dataclass_test

usage: json2dataclass_test [-h] [-d DATACLASS_DIR] [-j JSON_DIR]

options:
  -h, --help            show this help message and exit
  -d DATACLASS_DIR, --dataclass-dir DATACLASS_DIR
                        specify parsed dataclass directory to be test
  -j JSON_DIR, --json-dir JSON_DIR
                        specify json directory to be test
json2dataclass_test -j json -d dataclass

We can use 'json2dataclass_test' to check dataclass python quickly

Examples

from pyjson2dataclass import json2dataclass
import json


filepath = 'json/example1.json'

parse_text = json2dataclass('Example1', json.load(open(filepath)))
open('example1.py', 'w').write(parse_text)

[json/example1.json]

{
    "pascalName1": {
        "pascalName2": [
            1,
            2
        ]
    },
    "pascalName3": [
        {
            "pascalName4": [
                {
                    "pascalName5": {}
                }
            ]
        }
    ]
}

[example1.py]

from typing import Dict, List, Any, Optional
from dataclasses import dataclass


@dataclass
class PascalName1:
    pascal_name2: List[int]

    def __init__(self, data):
        self.pascal_name2 = [(int(i0)) for i0 in data.get("pascalName2")]


@dataclass
class PascalName4I:
    pascal_name5: dict

    def __init__(self, data):
        self.pascal_name5 = dict(data.get("pascalName5"))


@dataclass
class PascalName3I:
    pascal_name4: List[PascalName4I]

    def __init__(self, data):
        self.pascal_name4 = [(PascalName4I(i0)) for i0 in data.get("pascalName4")]


@dataclass
class Example1:
    pascal_name1: PascalName1
    pascal_name3: List[PascalName3I]

    def __init__(self, data):
        self.pascal_name1 = PascalName1(data.get("pascalName1"))
        self.pascal_name3 = [(PascalName3I(i0)) for i0 in data.get("pascalName3")]

Use example1.py

Run

import sys
sys.path.append('./dataclass')

from example1 import Example1
import json

filepath = 'json/example1.json'

example = Example1(json.load(open(filepath)))
print(example)
print(example.pascal_name1)
print(example.pascal_name3)

Output

Example1(pascal_name1=PascalName1(pascal_name2=[1, 2]), pascal_name3=[PascalName3I(pascal_name4=[PascalName4I(pascal_name5={})])])
PascalName1(pascal_name2=[1, 2])
[PascalName3I(pascal_name4=[PascalName4I(pascal_name5={})])]

Back to HomePage [https://github.com/verssionhack/pyjson2dataclass]

Metadata

Release files for pyjson2dataclass 2.1.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyjson2dataclass 2.1.9
File Size Uploaded Provenance Actions
pyjson2dataclass-2.1.9.tar.gz 34.5 kB Without provenance Details for pyjson2dataclass-2.1.9.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyjson2dataclass 2.1.9
File Interpreter ABI Platform Actions
pyjson2dataclass-2.1.9-py3-none-any.whl Python 3 none any Details for pyjson2dataclass-2.1.9-py3-none-any.whl

Total release size: 71.3 kB

Release files / pyjson2dataclass-2.1.9.tar.gz

Download URL pyjson2dataclass-2.1.9.tar.gz
Size 34.5 kB
Tags Source
SHA-256 checksum
How to use checksums
61c765febbf16b5e697c6c87e3ac47256c94bfaca00995a0c8c78cc79c82a967
BLAKE2b-256 checksum
How to use checksums
834408e65bf7a2ffc108b66dc3e695fecdf8756ce462e543049a691d4ae65831
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.8

Release files / pyjson2dataclass-2.1.9-py3-none-any.whl

Download URL pyjson2dataclass-2.1.9-py3-none-any.whl
Size 36.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ee2628f2120e2ab1b90d51c7b7e32bb9c9cdbdc18ac3634a238e64c3dcc7176f
BLAKE2b-256 checksum
How to use checksums
5d9693bb237458fc8cea3f3293da13d19f719a6ccce0fbd7652a0874a4a98b3a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.8

Security for pyjson2dataclass 2.1.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page