1
0

More than 3 years have passed since last update.

Node.jsのnpmモジュールを使ってjsonファイルをminify・整形する手順

Posted at

はじめに

Windows環境で、Node.jsのnpmモジュールを使って以下を行う手順をまとめます。

  • JSONファイルの軽量化(minify):改行やスペースの削除
  • JSONファイルの整形:改行やインデントにより見やすくする

使用モジュール

  • jsonminify → JSONファイルの軽量化モジュール
  • format-jsonfile → JSONファイルの整形出力モジュール

準備:モジュールのインストール

  1. Node.jsを取得しインストールする。→ https://nodejs.org/ja/

  2. Node.js command promptを起動する。

  3. 以下のコマンドを実行する。(2行目のプロキシ設定は必要な場合のみ実行)

$ npm -g config set registry http://registry.npmjs.org/
$ npm -g config set proxy http://xxx.xxx.xxx:8080
$ npm install -g json-minify
$ npm install -g format-jsonfile

使用方法

Node.js command promptから、以下のコマンドを実行する。

JSONファイルの軽量化

$ type test1.json
  [
    {
      "name": "foo",
      "age": 10
    },
    {
      "name": "bar",
      "age": 20
    },
    {
      "name": "hoge",
      "age": 30
    }
  ]
$ json-minify test1.json
  [{"name":"foo","age":10},{"name":"bar","age":20},{"name":"hoge","age":30}]

JSONファイルの整形

$ type test2.json
  [{"name":"foo","age":10},{"name":"bar","age":20},{"name":"hoge","age":30}]
$ format-jsonfile test2.json
  [
    {
      "name": "foo",
      "age": 10
    },
    {
      "name": "bar",
      "age": 20
    },
    {
      "name": "hoge",
      "age": 30
    }
  ]

参考記事

1
0
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
1
0