Skip to main content

Funções inteligentes para validação, limpeza e conversão de JSON em Python

Project description

📦 SmartJsonPy

SmartJsonPy é um pacote Python leve e poderoso para manipulação de JSON. Ele oferece funções para validação, limpeza, extração, deduplicamento e conversão de dados JSON. Ideal para preparação de dados, integrações com APIs e exportação para planilhas.


🧪 Testes

Execute os testes com pytest:

pytest tests/

🚀 Instalação

pip install SmartJsonPy

Requer: Python >= 3.7


✨ Funcionalidades

✅ Validação de JSON

from SmartJsonPy import validator

validator.is_valid_json_string('{"nome": "João"}')  # True
validator.is_valid_json_object({"id": 1})              # True
validator.has_required_keys({"id": 1}, ["id"])        # True
validator.is_json_schema_compatible({"id": 1}, {"id": int})  # True

🧹 Limpeza de dados

from SmartJsonPy import cleaner

data = {"a": 1, "b": None, "c": "", "d": []}
cleaner.remove_none_values(data)       # {"a": 1, "c": "", "d": []}
cleaner.remove_empty_values(data)      # {"a": 1}
cleaner.strip_whitespace_keys({" nome ": "Ana"})  # {"nome": "Ana"}

nested = {"x": {"y": {"z": None}}, "a": ["ok", {}]}
cleaner.clean_nested_dict(nested)  # {"a": ["ok"]}

🔍 Extração de dados

from SmartJsonPy import extractor

data = {"a": 1, "b": {"a": 2}, "c": [{"a": 3}, {"x": 9}]}
extractor.extract_keys(data)           # ['a', 'b', 'c', 'x']
extractor.extract_values_by_key(data, "a")  # [1, 2, 3]

🔁 Deduplicar dados

from SmartJsonPy import utils

# Dicionários duplicados
lst = [{"a": 1}, {"a": 1}, {"b": 2}]
utils.remove_duplicate_dicts(lst)  # [{"a": 1}, {"b": 2}]

# Itens duplicados
nums = [1, 2, 2, 3, 1]
utils.remove_duplicate_items(nums)  # [1, 2, 3]

🔄 Conversões entre string e JSON

from SmartJsonPy import utils

# JSON para string
d = {"nome": "João"}
utils.json_to_string(d)  # '{"nome": "João"}'

# String para JSON
s = '{"nome": "Maria"}'
utils.string_to_json(s)  # {'nome': 'Maria'}

🔄 Transformação de Dados

from SmartJsonPy import transformer

# JSON para YAML
data = {"nome": "João", "idade": 30}
yaml_str = transformer.json_to_yaml(data)
# name: João
# age: 30

# JSON para XML
xml_str = transformer.json_to_xml(data, root_name="pessoa")
# <?xml version="1.0" encoding="utf-8"?>
# <pessoa>
#   <nome>João</nome>
#   <idade>30</idade>
# </pessoa>

# JSON para CSV
lista = [
    {"nome": "João", "idade": 30},
    {"nome": "Maria", "idade": 25}
]
csv_str = transformer.json_to_csv(lista)
# nome,idade
# João,30
# Maria,25

# Achatamento de JSON
nested = {"a": {"b": {"c": 1}}}
flat = transformer.flatten_json(nested)
# {"a.b.c": 1}

# Desachatamento de JSON
nested = transformer.unflatten_json({"a.b.c": 1})
# {"a": {"b": {"c": 1}}}

📊 Conversão para formato de colunas (planilhas)

from SmartJsonPy import utils

lista = [
    {"name": "Roberto", "age": 45, "gender": "m"},
    {"name": "Joao", "age": 32, "gender": "m"},
    {"name": "Renata", "age": 39, "gender": "f"}
]

utils.convert_list_of_dicts_to_columns(lista)
# (
#   ('name', 'age', 'gender'),
#   ('Roberto', 'Joao', 'Renata'),
#   (45, 32, 39),
#   ('m', 'm', 'f')
# )

👨‍💻 Autor

Desenvolvido por Roberto Lima 🚀✨


💬 Contato


📄 Licença

MIT License

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

smartjsonpy-1.1.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

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

smartjsonpy-1.1.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file smartjsonpy-1.1.0.tar.gz.

File metadata

  • Download URL: smartjsonpy-1.1.0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for smartjsonpy-1.1.0.tar.gz
Algorithm Hash digest
SHA256 e7f7c8fdca4da6cc4c3d76777c0c5054162a731cec0347b46b93d8be84332b76
MD5 e694170bd4a8311eb4f3bb3799cb610c
BLAKE2b-256 f5cc6929670f8ab07ed5597b4b849903f814dc1ddcd7a124c5f78ef8630ae2b0

See more details on using hashes here.

File details

Details for the file smartjsonpy-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: smartjsonpy-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for smartjsonpy-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d075bf9101239c26ed54c8a6fe1d6e1de9f0a908d14f31f14979fa4bfa33b5b4
MD5 23873c6086002d27539727d89d5282af
BLAKE2b-256 6100a46202fae817d6d2471b68772b836104cd65ce899f159c541c6c6aedfeb1

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