hon
hone
pypi i hone
hon

hone

Convert CSV to automatically nested JSON

by Cham

0.2.1 (see all)License:MITCategories:Vanilla Python CSV to JSON
pypi i hone
Readme

hone

PyPI version PyPI license

Convert CSV to automatically nested JSON.

Table of Contents

Getting Started

Available as both a Python module and a command line tool.

Installation

pip install hone

Usage: Command Line

$ hone --help
usage: hone [-h] [-d [DELIMITERS]] [-s [SCHEMA]] csv_filepath json_filepath

positional arguments:
  csv_filepath          Specify the filepath for the file to read CSV data
                        from. To read from standard input, use a dash ("-") as
                        the value
  json_filepath         Specify the filepath for the file to output JSON data
                        to. To write to standard output, use a dash ("-") as
                        the value.

optional arguments:
  -h, --help            show this help message and exit
  -d [DELIMITERS], --delimiters [DELIMITERS]
                        Override the default delimiters for generating a
                        nested structure from column names. [DELIMITERS] must
                        be a Python-compatible list of strings. The default
                        value is [',', '_', ' '].
  -s [SCHEMA], --schema [SCHEMA]
                        Manually specify the schema that defines the structure
                        of the generated JSON, instead of having it
                        automatically generated. [SCHEMA] must be a valid JSON
                        object encoded as a string.

Usage: Python Module

import hone

optional_arguments = {
  "delimiters": [" ", "_", ","]
}
Hone = hone.Hone(**optional_arguments)
schema = Hone.get_schema('path/to/input.csv')  # nested JSON schema for input.csv
result = Hone.convert('path/to/input.csv', schema=schema)  # final structure, nested according to schema

Examples

You can view all examples of conversions in the examples directory.

CSV

namebirth daybirth monthbirth yearreferencereference name
Bob7May1985TRUESmith
Julia21January1997FALSEN/A
Rick12June1996TRUEClara

Generated JSON

[
  {
    "birth": {
      "day": "7",
      "month": "May",
      "year": "1985"
    },
    "name": "Bob",
    "reference": "TRUE",
    "reference name": "Smith"
  },
  {
    "birth": {
      "day": "21",
      "month": "January",
      "year": "1997"
    },
    "name": "Julia",
    "reference": "FALSE",
    "reference name": "N/A"
  },
  {
    "birth": {
      "day": "12",
      "month": "June",
      "year": "1996"
    },
    "name": "Rick",
    "reference": "TRUE",
    "reference name": "Clara"
  }
]

Development

Running tests

From the root directory of this repository, run python3 -m unittest to execute the entire test suite.

License

Hone is licensed under the MIT license.

GitHub Stars

125

LAST COMMIT

2yrs ago

MAINTAINERS

1

CONTRIBUTORS

1

OPEN ISSUES

12

OPEN PRs

1
VersionTagPublished
0.2.1
2yrs ago
0.2.0
2yrs ago
0.1.9
2yrs ago
0.1.8
2yrs ago
No tutorials found
Add a tutorial