LoginSignup
3
3

More than 5 years have passed since last update.

JSONのdiffを取る簡単なシェルスクリプト

Last updated at Posted at 2018-11-03

JSONのdiffを取るために、簡単なシェルスクリプトを書いた。
node.jsで引数のjsonを出力形式を整えてdiffに渡してる。
外部モジュールのインストール不要なのがメリット。

macOS High Sierraとmojaveで動作確認したがnode.jsとshellが動けば動くと思われる。
なお、引数のファイルパスは拡張子を.json(大文字でもOK)にする必要があります。

参考:https://qiita.com/bsdhack/items/55d5eced2fb3e6625d74

jsondiff.sh
#!/bin/sh

if [ $# -ne 2 ]; then
  echo "Usage:"
  echo "./jsondiff.sh a.json b.json"
  exit 1
fi

node -e "console.log(JSON.stringify(require('$1'),null,2))" | (node -e "console.log(JSON.stringify(require('$2'),null,2))" | diff -U 3 /dev/fd/3 -) 3<&0 

使い方

$ ./jsondiff.sh jsonのパス1 jsonのパス2
3
3
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
3
3