首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将JSON转换为表,标头为"Key“和"Value”

将JSON转换为表,标头为"Key“和"Value”
EN

Unix & Linux用户
提问于 2022-05-02 16:54:35
回答 1查看 3.6K关注 0票数 -1

我有以下JSON输出:

代码语言:javascript
复制
 [
  {
    "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"
  }
]

我一直在努力使输出看起来像一个反表。我可以让它看起来如下:

代码语言:javascript
复制
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

但有点乱,我想要的是:

代码语言:javascript
复制
Attribute           Value
---------           -----
enabled             true
policy_profile      custom
scan_local_files    true
...

任何帮助或指出一个现有的SE问题,包括这一具体问题,将不胜感激。

EN

回答 1

Unix & Linux用户

回答已采纳

发布于 2022-05-02 17:16:47

这似乎适用于您的输入数据。

代码语言:javascript
复制
jq -r '.[] | to_entries[] | [.key,.value] | @tsv' file.json

输出(选项卡分开):

代码语言:javascript
复制
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或其他工具以任何您想要的方式对其进行格式化。

如果要将AttributeValue列为第一行,请按以下方式更改命令

代码语言:javascript
复制
jq -r  '[ "Attribute", "Value"], ( .[] | to_entries[] | [.key,.value] ) | @tsv' file.json

参考jq:打印对象中每个条目的键和值,它刚刚向我介绍了to_entries

票数 0
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/701158

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档