Skip to main content

This is the `json-ops` package, intended to provide a collection of utilities for working with JSON data.

Project description

json-ops

This is the json-ops package, intended to provide a collection of utilities for working with JSON data. Currently, it includes functionality to convert CSV and Excel files and strings to JSON format. More features will be added in the future.

Installation

No specific installation is required. Ensure you have Python and the pandas library installed. If you intend to run the tests, you will also need pytest and openpyxl:

pip install pandas openpyxl pytest

Modules

csv_to_json.py

This script provides functionality to convert CSV (Comma Separated Values) files and strings to JSON (JavaScript Object Notation) format.

Features

  • Converts CSV files to JSON files.
  • Converts CSV strings to JSON strings.
  • Handles basic data type conversions (integer, float, boolean).

Usage

Converting a CSV file to JSON
from json_converter.csv_to_json import convert_csv_file

input_file = 'path/to/your/input.csv'
output_file = 'path/to/your/output.json'

success = convert_csv_file(input_file, output_file)

if success:
    print(f"Successfully converted {input_file} to {output_file}")
else:
    print(f"Failed to convert {input_file}")
Converting a CSV string to JSON
from json_converter.csv_to_json import convert_csv_string

csv_string = "Name,Age,City\\nAlice,30,New York\\nBob,25,London"
json_string = convert_csv_string(csv_string)

if json_string:
    print(json_string)
else:
    print("Failed to convert CSV string")

Limitations

  • No support for nested input files: This converter is designed to work with flat CSV files. It does not support CSV files with nested structures or hierarchical data.

excel_to_json.py

This script provides functionality to convert Excel files and strings to nested JSON format based on column names.

Features

  • Converts Excel files to nested JSON files.
  • Converts Excel file content (bytes) to nested JSON strings.
  • Uses column names with dot notation (e.g., address.city) to create nested JSON structures.

Usage

Converting an Excel file to nested JSON
from json_converter.excel_to_json import convert_excel_file

input_file = 'path/to/your/input.xlsx'
output_file = 'path/to/your/output.json'

success = convert_excel_file(input_file, output_file)

if success:
    print(f"Successfully converted {input_file} to {output_file}")
else:
    print(f"Failed to convert {input_file}")
Converting Excel file content (bytes) to nested JSON
from json_converter.excel_to_json import convert_excel_string
import pandas as pd
import io

data = {'address.city': ['New York', 'London'], 'name': ['Alice', 'Bob']}
df = pd.DataFrame(data)
buffer = io.BytesIO()
df.to_excel(buffer, index=False)
excel_content_bytes = buffer.getvalue()

json_string = convert_excel_string(excel_content_bytes)

if json_string:
    print(json_string)
else:
    print("Failed to convert Excel content")

Limitations

  • Requires column names to follow a dot notation convention for creating nested structures.
  • Handles basic data types as inferred by pandas.

Running Tests

To run the tests for the modules:

pytest tests/test_csv_to_json.py
pytest tests/test_excel_to_json.py

License

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

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

json_ops-0.1.1.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

json_ops-0.1.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file json_ops-0.1.1.tar.gz.

File metadata

  • Download URL: json_ops-0.1.1.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.1

File hashes

Hashes for json_ops-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3ef03a77d140bd9052f7bb807733800d74094ccbd56cba6c373804f56224a95b
MD5 bec269ec7d3bbc120796bf9b644b5e04
BLAKE2b-256 5e4c837122722b9f8929c45f5a5e4ebe63ff90ed3d5037c51445ebbf71355219

See more details on using hashes here.

File details

Details for the file json_ops-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: json_ops-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.1

File hashes

Hashes for json_ops-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ba3ba87da14e3b05bc5adf66e465a5850a976b51c73292f45fc21a979fd7a2e5
MD5 7c8bfa8669afce50981761bbd0f23227
BLAKE2b-256 38dc2f360f84e76cf7ccd044597f17e45d6b1201cf8786b802db563fcabf857f

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