Skip to main content

Parse json to dataclass

Project description

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('example.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):
        if data is None:
            return None
        self.pascal_name2 = [(int(i0)) for i0 in data.get("pascalName2")]


@dataclass
class PascalName4Item:
    pascal_name5: dict

    def __init__(self, data):
        if data is None:
            return None
        self.pascal_name5 = dict(data.get("pascalName5"))


@dataclass
class PascalName3Item:
    pascal_name4: List[PascalName4Item]

    def __init__(self, data):
        if data is None:
            return None
        self.pascal_name4 = [(PascalName4Item(i0)) for i0 in data.get("pascalName4")]


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

    def __init__(self, data):
        if data is None:
            return None
        self.pascal_name1 = PascalName1(data.get("pascalName1"))
        self.pascal_name3 = [(PascalName3Item(i0)) for i0 in data.get("pascalName3")]                                                                                                                     

Use example.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=[PascalName3Item(pascal_name4=[PascalName4Item(pascal_name5={})])])
PascalName1(pascal_name2=[1, 2])
[PascalName3Item(pascal_name4=[PascalName4Item(pascal_name5={})])]

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyjson2dataclass-2.0.9.tar.gz (22.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyjson2dataclass-2.0.9-py3-none-any.whl (22.4 kB view details)

Uploaded Python 3

File details

Details for the file pyjson2dataclass-2.0.9.tar.gz.

File metadata

  • Download URL: pyjson2dataclass-2.0.9.tar.gz
  • Upload date:
  • Size: 22.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for pyjson2dataclass-2.0.9.tar.gz
Algorithm Hash digest
SHA256 7617d6c813b0347f6acbd4aec3e2789bfb72ea40d4aa1334cc22273b471c1005
MD5 29c42f6d06dba6ce2966ac484396d68f
BLAKE2b-256 fc6d48b92a2bb0902018962cbd0ccb5546e5a40e051bae40c118216ec40ba117

See more details on using hashes here.

File details

Details for the file pyjson2dataclass-2.0.9-py3-none-any.whl.

File metadata

File hashes

Hashes for pyjson2dataclass-2.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 a5d22d99540ca65569c99cd0169abaec8d772eadb54222b0b7681e43caa3540e
MD5 6938c98c3d5ed705951dfa38f23f30a3
BLAKE2b-256 1c06e03c56b176c2c74426549540d98430cd99c0a593c9e09f5adfe356e6e6e7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page