Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
name: CI

on:
push:
branches: [master, main]
pull_request:
branches: [master, main]
workflow_dispatch:

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install uv
run: |
curl -LsSf https://astral.sh/uv/install.sh | sh
echo "$HOME/.local/bin" >> $GITHUB_PATH
- name: Set up UV cache
uses: actions/cache@v4
with:
path: .venv
key: uv-${{ runner.os }}-lint-${{ hashFiles('pyproject.toml', 'requirements.txt') }}
restore-keys: |
uv-${{ runner.os }}-lint-
- name: Create virtual environment
run: uv venv || true
- name: Install dependencies
run: uv pip install -e ".[dev]"
- name: Run ruff
run: uv run ruff check .
- name: Run mypy
run: uv run mypy jsoncsv

test:
name: Test (Python ${{ matrix.python-version }})
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install uv
run: |
curl -LsSf https://astral.sh/uv/install.sh | sh
echo "$HOME/.local/bin" >> $GITHUB_PATH
- name: Set up UV cache
uses: actions/cache@v4
with:
path: .venv
key: uv-${{ runner.os }}-py${{ matrix.python-version }}-${{ hashFiles('pyproject.toml', 'requirements.txt') }}
restore-keys: |
uv-${{ runner.os }}-py${{ matrix.python-version }}-
- name: Create virtual environment
run: uv venv || true
- name: Install dependencies
run: uv pip install -e ".[dev]"
- name: Run tests
run: uv run pytest --cov=jsoncsv --cov-report=xml
- name: Upload coverage
uses: codecov/codecov-action@v4
with:
file: ./coverage.xml
fail_ci_if_error: false

build:
name: Build package
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install uv
run: |
curl -LsSf https://astral.sh/uv/install.sh | sh
echo "$HOME/.local/bin" >> $GITHUB_PATH
- name: Set up UV cache
uses: actions/cache@v4
with:
path: .venv
key: uv-${{ runner.os }}-build-${{ hashFiles('pyproject.toml') }}
restore-keys: |
uv-${{ runner.os }}-build-
- name: Create virtual environment
run: uv venv || true
- name: Build package
run: |
uv pip install build twine
uv run python -m build
- name: Check package
run: uv run twine check dist/*
20 changes: 20 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Pre-commit configuration for jsoncsv
# Install: pip install pre-commit
# Run: pre-commit install

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.9.0
hooks:
- id: ruff
args: [--fix]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.14.1
hooks:
- id: mypy
additional_dependencies:
- click
- xlwt
args: [--ignore-missing-imports]
28 changes: 0 additions & 28 deletions .travis.yml

This file was deleted.

204 changes: 204 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
# jsoncsv : easily convert json to csv or xls[x]

[![PyPI](https://img.shields.io/pypi/v/jsoncsv.svg)](https://pypi.python.org/pypi/jsoncsv)
[![CI](https://github.com/alingse/jsoncsv/actions/workflows/ci.yml/badge.svg)](https://github.com/alingse/jsoncsv/actions/workflows/ci.yml)
[![Coverage](https://codecov.io/gh/alingse/jsoncsv/badge.svg)](https://codecov.io/gh/alingse/jsoncsv)
[![Ruff](https://img.shields.io/endpoint?url=https://ruff.ai/api/badge)](https://github.com/astral-sh/ruff)

jsoncsv (with `mkexcel`) is a command tool to convert json file to csv/xlsx file.

It's simple, and no need user to specify the keys.

Just use them.

## Requirements

- Python 3.8 or higher

## Quick Start

Cat the raw.json to csv/xls use command line tool

```bash
cat raw.json | jsoncsv | mkexcel > output.csv
cat raw.json | jsoncsv | mkexcel -t xls > output.xls
```

Each line of raw json text file is a json object

```json
{"id":1, "name":"A", "year": 2015}
{"id":2, "name":"S", "zone": "china"}
```

Jsoncsv will output a csv file

```
id,name,year,zone
1,A,2015,
2,S,,china
```

This is easily and needn't care the different keys from any two object.

### For Json Array

If input file is an json_array, use `-A/--array` to decode it

```bash
cat raw.json | jsoncsv -A | mkexcel > output.csv
```

**Input File**

```json
[
{"id":1, "name":"A", "year": 2015},
{"id":2, "name":"S", "zone": "china"}
]
```

**Output File**

```
id,name,year,zone
1,A,2015,
2,S,,china
```

### Step by Step

```bash
jsoncsv raw.json expand.json
mkexcel expand.json -t xls output.xls
```

get more options with `--help`.

```bash
jsoncsv --help
mkexcel --help
```

## Install

Using pip (recommended):

```bash
pip install jsoncsv
```

Using uv (faster):

```bash
uv pip install jsoncsv
```

For development:

```bash
git clone https://github.com/alingse/jsoncsv
cd jsoncsv
uv pip install -e ".[dev]"
```

## Usage

### Expand/Restore JSON

Just expand/restore the json, the expand json is one layer json.

```bash
jsoncsv raw.json expand.json
jsoncsv -r expand.json raw.json
cat raw.json | jsoncsv | jsoncsv -r > raw2.json
```

### Export to Excel/CSV

mkexcel the expanded json (one layer)

```bash
mkexcel expand.json output.csv
mkexcel -t xls expand.json > output.xls
mkexcel -t csv expand.json > output.csv
```

## Options

### -e, --expand

expand json, 展开 json

```bash
jsoncsv -e raw.json expand.json
cat raw.json expand.json
{"s":[1,2,{"w":1}]}
{"s.2.w": 1,"s.0": 1,"s.1": 2}
```

`{"s":[1,2,{"w":1}]}` will transformed to `{"s.2.w": 1,"s.0": 1,"s.1": 2}`

the output "expand.json" is only one layer json, it can be easy change to csv or xlsx (with `mkexcel`)

### -r, --restore

restore the expanded json 重构被展开的json

```bash
jsoncsv -r expand.json raw.json
cat expand.json raw.json
{"s.2.w": 1,"s.0": 1,"s.1": 2}
{"s": [1, 2, {"w": 1}]}
```

`{"s.2.w": 1,"s.0": 1,"s.1": 2}` change to `{"s":[1,2,{"w":1}]}`

### -s, --separator

separator used to combine the keys in the tree

default separator is **.**

### --safe

on safe mode, use escape separator to avoid confilct

expand:

`['aa', 'bb', 'www.xxx.com']` --> `'aa\\.bb\\.www.xxx.com'`

restore:

`'aa\\.bb\\.www.xxx.com'` --> `['aa', 'bb', 'www.xxx.com']`

## Development

Lint and format:

```bash
ruff check .
ruff format .
```

Run tests:

```bash
pytest
```

Type checking:

```bash
mypy jsoncsv
```

Build package:

```bash
python -m build
```

## License

Apache License 2.0
Loading