jq で値が null のカラムを除外する
こんにちは、 @kz_morita です。
今回は jq を使っていて、Json の null が入っているカラムを削除したいときの Tips をメモしておきます。
値が null のフィールドを削除する 以下のようにすると、null のフィールドを削除できます。
$ cat sample.json | jq 'to_entries | map(select(.value != null)) | from entries' 以下解説です。
まず今回の対象の json は以下のようなものです。
{ "name": "Alice", "age": null, "address": { "street": "123 Main St", "city": "Wonderland" }, "phoneNumbers": [ { "type": "home", "number": "555-1234" } ], "isActive": true, "preferences": null } これが上記のコマンドを使用すると以下のようになります。
{ "name": "Alice", "address": { "street": "123 Main St", "city": "Wonderland" }, "phoneNumbers": [ { "type": "home", "number": "555-1234" } ], "isActive": true } preferences と age カラムが消えていることがわかります。