3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

APIで取得したJSONをdiffコマンドで差分比較する

Last updated at Posted at 2023-01-13

はじめに

業務で複数の類似システムの設定値をAPIで取得し、差分比較で設定の一致をまとめて確かめた際の手順を示します。

環境

Ubuntu 20.04

必要コマンドのインストール

本記事ではcurl,diff,jqコマンドを使用します。
インストールされていない場合はインストールします。

$ curl --version
curl 7.68.0 (x86_64-pc-linux-gnu) libcurl/7.68.0 OpenSSL/1.1.1f zlib/1.2.11 brotli/1.0.7 libidn2/2.2.0 libpsl/0.21.0 (+libidn2/2.2.0) libssh/0.9.3/openssl/zlib nghttp2/1.40.0 librtmp/2.3
Release-Date: 2020-01-08
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp scp sftp smb smbs smtp smtps telnet tftp 
Features: AsynchDNS brotli GSS-API HTTP2 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM NTLM_WB PSL SPNEGO SSL TLS-SRP UnixSockets


$ diff --version 
diff (GNU diffutils) 3.7
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

作者 Paul Eggert、 Mike Haertel、 David Hayes、
Richard Stallman、および Len Tower。

$ jq --version
jq-1.6

APIでJSONを取得

利用するAPIは何でも構いませんが、今回は郵便番号検索APIを利用してみたいと思います。

# パラメータの郵便番号に対応する住所が返される
$ curl "https://zipcloud.ibsnet.co.jp/api/search?zipcode=1080014"
{
	"message": null,
	"results": [
		{
			"address1": "東京都",
			"address2": "港区",
			"address3": "芝",
			"kana1": "トウキョウト",
			"kana2": "ミナトク",
			"kana3": "シバ",
			"prefcode": "13",
			"zipcode": "1080014"
		}
	],
	"status": 200
}

比較するための適当な2地点のJSONをファイルに出力します。
今回は近い二地点の物を利用します。

$ curl "https://zipcloud.ibsnet.co.jp/api/search?zipcode=1080014 > shiba"
$ curl "https://zipcloud.ibsnet.co.jp/api/search?zipcode=1050003 > nishishimbashi"

# address3,kana3,zipcodeが違う
$ cat shiba 
{
	"message": null,
	"results": [
		{
			"address1": "東京都",
			"address2": "港区",
			"address3": "芝",
			"kana1": "トウキョウト",
			"kana2": "ミナトク",
			"kana3": "シバ",
			"prefcode": "13",
			"zipcode": "1080014"
		}
	],
	"status": 200
}
$ cat nishishimbashi 
{
	"message": null,
	"results": [
		{
			"address1": "東京都",
			"address2": "港区",
			"address3": "西新橋",
			"kana1": "トウキョウト",
			"kana2": "ミナトク",
			"kana3": "ニシシンバシ",
			"prefcode": "13",
			"zipcode": "1050003"
		}
	],
	"status": 200
}

この後diffコマンドで差分比較をし、一致部分の確認をしますが、この時APIで取得したJSONが整形されていないことがあります。

そのような場合はjqコマンドを使用します。
cat xxx | jq .のように使用することで、JSONを成形することが出来ます。

$ echo '{"name": "Tom", "class" : 1}' > tom
$ echo '{"name": "Bob", "class" : 1}' > bob

# 整形されていないと一致しない1行としてみなされてしまう
$ diff tom bob 
1c1
< {"name": "Tom", "class" : 1}
---
> {"name": "Bob", "class" : 1}

# 整形
$ cat tom | jq .
{
  "name": "Tom",
  "class": 1
}

cat tom | jq . > tomjq
cat bob | jq . > bobjq

$ diff tomjq bobjq
2c2
<   "name": "Tom",
---
>   "name": "Bob",

整形されたJSONをdiffコマンドで差分比較します。
diff ファイル1 ファイル2のように使用します。

$ diff shiba nishishimbashi 
7c7
< 			"address3": "芝",
---
> 			"address3": "西新橋",
10c10
< 			"kana3": "シバ",
---
> 			"kana3": "ニシシンバシ",
12c12
< 			"zipcode": "1080014"
---
> 			"zipcode": "1050003"

差分だけ比較することが出来ました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?