0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Excel(CSV)の設定データをJSON/YAMLに変換するCLIをRustで作った

0
Posted at

実務で次のような作業をすることがあります。
• Excelで管理された設定をJSONに変換
• Excelで管理された設定をYAMLに変換

例えば
• DynamoDBインポート用JSON作成
• AWS CDKの設定ファイル作成

元データはExcelで管理されていることが多いので、実際の作業は次のようになります。

Excel → CSV → JSON / YAML

最初はPythonスクリプトを書いて対応していましたが、
• 用途ごとにスクリプトが増える
• 微妙に仕様が違う
• メンテナンスが面倒

という問題が出てきました。

そこで CSV → 設定ファイル変換用のCLIツールを作りました。

ツール名は tabstruct です。

tabstruct

CSV / JSON / YAML の相互変換ができるCLIツールです。

主な用途
• CSV → JSON
• CSV → YAML
• JSON → CSV
• YAML → CSV
• JSON ↔ YAML
• データ構造確認

インストール

(GitHub release など)

brew install tabstruct

または

cargo install tabstruct

使い方

CSV → JSON

tabstruct convert --file config.csv --json

CSV → YAML

tabstruct convert --file config.csv --yaml

JSON → CSV

tabstruct convert --file config.json --csv

CSVでネスト構造を扱う

CSVではドット記法でネスト構造を表現します。

CSV

id,name,settings.interval,settings.url
1,canary-a,5,https://example.com

JSON

{
  "id": 1,
  "name": "canary-a",
  "settings": {
    "interval": 5,
    "url": "https://example.com"
  }
}

この方法でCSVでもネスト構造を扱えます。

schemaコマンド

変換前にデータ構造を確認できます。

tabstruct schema config.csv

出力

Format: CSV
Root Type: array
Records: 3

Fields:
- id: integer
- name: string
- settings.interval: integer
- settings.url: string

GitHub

https://github.com/kyotalab/tabstruct

まとめ

Excel(CSV)で設定を管理するケースは多いですが、

CSV → JSON / YAML

変換作業が毎回必要になります。

用途ごとにスクリプトを書くより、

汎用CLIツールを用意した方が楽だと思い作りました。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?