This is the `json-ops` package, intended to provide a collection of utilities for working with JSON data.
Project description
json-ops
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file json_ops-0.1.2.tar.gz.
File metadata
- Download URL: json_ops-0.1.2.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
24736fe54577b9ab24934f227841f90df64dbd6754b9cb432cdb810867085497
|
|
| MD5 |
765fe4fa5f9b586a34e914b80c0e95cc
|
|
| BLAKE2b-256 |
1785be7a99b1e3a65fb522bb2a0c8de5a8115e196c1e44b9ff4b1be1d4848ea7
|
File details
Details for the file json_ops-0.1.2-py3-none-any.whl.
File metadata
- Download URL: json_ops-0.1.2-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e20c1ce90c12f04a13865f7e1d5ce0d4cf5c9e0048823a77aa408b40cc228422
|
|
| MD5 |
ed200c6c138b17e4e8d763086d811928
|
|
| BLAKE2b-256 |
5a06616f503c360840b07c0627d748316b2dd70985f6ea7d2ac3ae1498878abb
|