3
2

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 5 years have passed since last update.

json ファイルの不要なカンマを除去

Last updated at Posted at 2015-07-16

こんにちは。
json ファイルに対して不要なカンマを除去してみました。すなわちカンマの後に括弧が閉じられていて json として不正なデータになっている場合に対する修正です。

$ ruby -e 'print ARGF.read.gsub(/,(?=\s*[}\]])/, "")' bad.json > good.json
$ jsonlint -v good.json
good.json: ok
$ cat bad.json
{
  "features": [
    "properties": {
      "level": 0,
    },
  ],
}

上記はコメントで頂いたものでして、推奨だと思いました。本稿内容も修正しています。標準入力渡しでも大丈夫ですね。

下記は元々の私自身のものですが、他人へは勧められないですね。

$ ruby -e 'print IO.readlines(ARGV[0], nil)[0].gsub(/,(\s+[}\]])/, "\\1")' bad.json > good.json
3
2
3

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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?