JSON转换为表格格式从-http://json2table.com获取 输入:输出未过帐:

如何解决JSON转换为表格格式从-http://json2table.com获取 输入:输出未过帐:

我目前正在尝试将AWS ECR扫描集成到我们的CI / CD管道,并将结果以易于阅读的形式传递给我们的工程师。

命令-aws ecr describe-image-scan-findings --repository-name ${REPNAME} --image-id imageTag=latest --profile ${PROFILE} --region ${REGION}

返回类似以下[已编辑]输出的内容-

    "imageScanFindings": {
        "findings": [
            {
                "name": "CVE-2018-12886","description": "stack_protect_prologue in cfgexpand.c and stack_protect_epilogue in function.c in GNU Compiler Collection (GCC) 4.1 through 8 (under certain circumstances) generate instruction sequences when targeting ARM targets that spill the address of the stack protector guard,which allows an attacker to bypass the protection of -fstack-protector,-fstack-protector-all,-fstack-protector-strong,and -fstack-protector-explicit against stack overflow by controlling what the stack canary is compared against.","uri": "https://security-tracker.debian.org/tracker/CVE-2018-12886","severity": "MEDIUM","attributes": [
                    {
                        "key": "package_version","value": "8.3.0-6"
                    },{
                        "key": "package_name","value": "gcc-8"
                    },{
                        "key": "CVSS2_VECTOR","value": "AV:N/AC:M/Au:N/C:P/I:P/A:P"
                    },{
                        "key": "CVSS2_SCORE","value": "6.8"
                    }
                ]
            },{
                "name": "CVE-2020-1751","description": "An out-of-bounds write vulnerability was found in glibc before 2.31 when handling signal trampolines on PowerPC. Specifically,the backtrace function did not properly check the array bounds when storing the frame address,resulting in a denial of service or potential code execution. The highest threat from this vulnerability is to system availability.","uri": "https://security-tracker.debian.org/tracker/CVE-2020-1751","value": "2.28-10"
                    },"value": "glibc"
                    },"value": "AV:L/AC:M/Au:N/C:P/I:P/A:C"
                    },"value": "5.9"
                    }
                ]
            },{
                "name": "CVE-2019-20367","description": "nlist.c in libbsd before 0.10.0 has an out-of-bounds read during a comparison for a symbol name from the string table (strtab).","uri": "https://security-tracker.debian.org/tracker/CVE-2019-20367","value": "0.9.1-2"
                    },"value": "libbsd"
                    },"value": "AV:N/AC:L/Au:N/C:P/I:N/A:P"
                    },"value": "6.4"
                    }
                ]
            },{
                "name": "CVE-2019-12904","description": "In Libgcrypt 1.8.4,the C implementation of AES is vulnerable to a flush-and-reload side-channel attack because physical addresses are available to other processes. (The C implementation is used on platforms where an assembly-language implementation is unavailable.)","uri": "https://security-tracker.debian.org/tracker/CVE-2019-12904","value": "1.8.4-5"
                    },"value": "libgcrypt20"
                    },"value": "AV:N/AC:M/Au:N/C:P/I:N/A:N"
                    },"value": "4.3"
                    }
                ]
            }
        ],"imageScanCompletedAt": "2020-10-23T00:03:10+05:30","vulnerabilitySourceUpdatedAt": "2020-10-22T16:21:44+05:30","findingSeverityCounts": {
            "MEDIUM": 14,"INFORMATIONAL": 72,"LOW": 18,"UNDEFINED": 3
        }
    },"registryId": "12345678911","repositoryName": "name-of-repo","imageId": {
        "imageDigest": "sha256:1213412412412451241414214141412412","imageTag": "latest"
    },"imageScanStatus": {
        "status": "COMPLETE","description": "The scan was completed successfully."
    }
}

上面的内容不便于阅读,特别是在发现很多内容并且JSON输出运行数百行的情况下。

我想将上述输出转换为更具“人类”可读性的形式,而不忽略任何返回的信息。我尝试对--output table使用AWS CLI选项,但是在列和行之间包含很多空格。

我尝试使用jq使用map将其转换为某种形式的表或.tsv,但是运气不好,因为我是JQ的初学者。如果有人对如何解决此问题有任何想法,我们将不胜感激。

旨在从我从http://json2table.com/-

获得的以下内容中获得一些帮助:

Expected Formatted Output

解决方法

以下根据我的要求生成了一个带有嵌套子表的表 了解基本要求。

示例

为阐明主要功能json2tree的作用,下面是一个示例:

输入:

[
  {"a": 1,"b": 2,"ary":[{"c":0,"d":1},{"c":10,"d":11}],"another":[{"a":1,"b":2}]},{"a":10,"b":20,"c":30,"ary":[{"e":0,"f":1},{"g":10,"h":11}],"xyzzy":"x"}
 ] 

输出(未过帐):

a       b       ary                             another         c       xyzzy
1       2       c       d                       a       b       null    null
                0       1                       1       2               
                10      11                                              
10      20      e       f       g       h                       30      x
                0       1       null    null                            
                null    null    10      11                              
            

假设

撇开错误,有关输入数据的唯一假设如下:

  1. 输入是JSON对象数组$ a;
  2. 如果$ a [$ i] [$ k]是一些$ i和一些键$ k的JSON对象数组, 那么$ a [$ j] [$ k]是所有$ j
  3. 的JSON对象数组

健壮

实现的复杂性(或至少是长度)是其相对于输入数据相当可靠的结果。例如,不要求顶级对象具有相同的键,也不必以一致的顺序指定它们。

但是,以每当非标量调用tostring的代价来实现这种鲁棒性 在上述(2)所未设想的上下文中找到JSON实体。

助手功能

# determine if the input is an array of JSON objects
def is_array_of_objects: type=="array" and all(.[]; type=="object");

# input: a JSON object
def reorder_keys($object): . as $in | $object | with_entries(.value = $in[.key]);

# input: an array of one or more objects
# output: an array of similar objects but altered by adding null values so that they all have the same keys,#         and so that the keys are in the same order
def synthesize:
  (add|map_values(null)) as $a
  | map($a + .)
  | .[0] as $template
  | map(reorder_keys($template));

# Input: an array of conformal objects,i.e.,with the same keys,#        and such that if .[$i][$k] is an array of objects for any $i or $i,then .[$j][$k] is 
#        also an array of objects for all $j
# Output: an object with the same keys but with the value replaced by the max `length` for an array of objects,#        and -1 otherwise.
def object_of_widths:
  def w: map_values(if is_array_of_objects then .[0]|length else -1 end);
  def keywise_maximum($x; $y): reduce ($y|keys_unsorted)[] as $k ({}; .[$k] = ([$x[$k],$y[$k]]|max));
  reduce (.[]|w) as $w ({}; keywise_maximum(.; $w));

主程序

# Input: an array of objects
# Output: a table that treats top-level arrays of JSON objects specially
def json2tree:

  # input: an array
  # output: an array of length at least $n
  def rpad($n): . + [range(0;$n-length)|null];
  
  # input: an object
  def print_first_line($keys; $widths):
    [$keys[] as $k | $k,(if $widths[$k] == -1 then empty else range(1;$widths[$k]) | null end)];
    
  # input: an object
  def print_second_line($keys; $widths):
    [$keys[] as $k
    | if $widths[$k] == -1
      then .[$k]|tostring
      elif .[$k] then .[$k][0]|keys_unsorted|rpad($widths[$k])[]
      else range(0; $widths[$k]) | null
      end];

  # input: an object
  # $i - the $i-th object in the array
  def print_nth_line($i; $keys; $widths):
    [$keys[] as $k
    | if $widths[$k] == -1
      then null
      elif $i < (.[$k]|length)
      then (.[$k][$i] | map(tostring) | rpad($widths[$k]))[]
      else (range(0;$widths[$k])|null)
      end] ;

  synthesize
  | map(map_values(if is_array_of_objects then synthesize else . end))
  | (.[0] | keys_unsorted) as $keys
  | object_of_widths as $widths
  | (.[0] | print_first_line($keys; $widths)),(range(0; length) as $ix
     | (.[$ix] | print_second_line($keys; $widths)),(.[$ix]
        | ([.[] | if is_array_of_objects then length else 0 end] | max) as $mx
        | range(0; $mx) as $i | print_nth_line($i; $keys; $widths) ) ) ;

json2tree | @tsv

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-