JSONをヘッダー「キー」と「値」を持つテーブルに変換する

JSONをヘッダー「キー」と「値」を持つテーブルに変換する

次のJSON出力があります。

 [
  {
    "enabled": "true",
    "policy_profile": "custom",
    "scan_local_files": "true",
    "local_file_types": "all",
    "scan_network_files": "false",
    "limit_file_size": "false",
    "enable_archive_scanning": "false",
    "scan_boot_sectors": "true",
    "scan_only_new_changes": "true",
    "scan_for_keyloggers": "true",
    "scan_for_puas": "true",
    "deferred_scanning": "true",
    "scan_action_for_infected_files": "Move to quarantine",
    "scan_action_for_infected_files_secondary": "Move to quarantine",
    "scan_action_for_suspect_files": "Move to quarantine",
    "scan_action_for_suspect_files_secondary": "Deny Access"
  }
]

私は出力を逆テーブルのように見せるのに苦労しました。次のように作成できます。

deferred_scanning       enable_archive_scanning enabled limit_archive_size      limit_file_size local_file_types        max_archive_depth       policy_profile  scan_action_for_infected_files  scan_action_for_infected_files_secondary   scan_action_for_suspect_files   scan_action_for_suspect_files_secondary scan_boot_sectors       scan_for_keyloggers     scan_for_puas   scan_local_files  scan_network_files       scan_only_new_changes
-----------------       ----------------------- ------- ------------------      --------------- ----------------        -----------------       --------------  ------------------------------  ----------------------------------------   -----------------------------   --------------------------------------- -----------------       -------------------     -------------   ----------------  ------------------       ---------------------
true    true    true    5       false   all     6       custom  Move to quarantine      Move to quarantine      Move to quarantine      Deny Access     true    true    true    true    false   true

しかし、これは少し厄介です。私は次のようなものが欲しい:

Attribute           Value
---------           -----
enabled             true
policy_profile      custom
scan_local_files    true
...

この特定の問題に対処する既存のSEの質問に対する助言やアドバイスをいただきありがとうございます。

ベストアンサー1

これは入力データで動作するようです

jq -r '.[] | to_entries[] | [.key,.value] | @tsv' file.json

出力(タブ区切り):

enabled true
policy_profile  custom
scan_local_files        true
local_file_types        all
scan_network_files      false
limit_file_size false
enable_archive_scanning false
scan_boot_sectors       true
scan_only_new_changes   true
scan_for_keyloggers     true
scan_for_puas   true
deferred_scanning       true
scan_action_for_infected_files  Move to quarantine
scan_action_for_infected_files_secondary        Move to quarantine
scan_action_for_suspect_files   Move to quarantine
scan_action_for_suspect_files_secondary Deny Access

次に、比較的簡単なawkツールやその他のツールを使用して、必要に応じてフォーマットします。

Attribute列が最初の行になるようにするには、Value次のようにコマンドを変更します。

jq -r  '[ "Attribute", "Value"], ( .[] | to_entries[] | [.key,.value] ) | @tsv' file.json

引用するjq: オブジェクトの各項目のキーと値を印刷します。、先ほど紹介しましたto_entries

おすすめ記事