简体   繁体   English

jq - 循环通过 json object 并且只显示某些值

[英]jq - looping through json object and only displaying certain values

I am currently learning how to usage jq .我目前正在学习如何使用jq I have a json object and I am able to loop and read values with jq as such cat test.json | jq -r '.test'我有一个 json object 并且我可以使用jq循环和读取值,例如cat test.json | jq -r '.test' cat test.json | jq -r '.test' . cat test.json | jq -r '.test' However, I am running into some complexity when I only want to display on values that have outdated or deprecated = true but ommit from result if relase = never-update-app or never-upgrade-app .但是,当我只想显示已outdateddeprecated = true但如果relase = never-update-appnever-upgrade-app从结果中省略的值时,我遇到了一些复杂性。 I am trying cat test.json | jq '.test | select(.[].outdated==true)'我正在尝试cat test.json | jq '.test | select(.[].outdated==true)' cat test.json | jq '.test | select(.[].outdated==true)' cat test.json | jq '.test | select(.[].outdated==true)' but this is not returning the desired results. cat test.json | jq '.test | select(.[].outdated==true)'但这不会返回所需的结果。 Is this something possible through jq and have it display in the below desired format?这是否可以通过 jq 实现并以以下所需格式显示?

My desired output is shown below:我想要的 output 如下图所示:

Release Name                                             Installed    Latest    Old      Deprecated
test-app                                                 1.0.0        2.0.0     true     false  

json: json:

{
    "test": [{
        "release": "myapp1",
        "Installed": {
            "version": "0.3.0",
            "appVersion": "v1.2.6"
        },
        "Latest": {
            "version": "",
            "appVersion": ""
        },
        "outdated": false,
        "deprecated": false
    }, {
        "release": "myapp2",
        "Installed": {
            "version": "6.5.13",
            "appVersion": "1.9.1"
        },
        "Latest": {
            "version": "",
            "appVersion": ""
        },
        "outdated": false,
        "deprecated": false
    }, {
        "release": "test-app",
        "Installed": {
            "version": "1.0.0",
            "appVersion": ""
        },
        "Latest": {
            "version": "2.0.0",
            "appVersion": ""
        },
        "outdated": true,
        "deprecated": false
    }, {
        "release": "never-update-app",
        "Installed": {
            "version": "1.0.0",
            "appVersion": ""
        },
        "Latest": {
            "version": "3.0.0",
            "appVersion": ""
        },
        "outdated": true,
        "deprecated": false
    }, {
        "release": "never-upgrade-app",
        "Installed": {
            "version": "2.0.0",
            "appVersion": ""
        },
        "Latest": {
            "version": "2.0.0",
            "appVersion": ""
        },
        "outdated": false,
        "deprecated": true
    }]
}

How to act on each element in an array如何作用于数组中的每个元素

You have an array in .test .您在.test中有一个数组。 If you want to extract the individual elements of that array to use in the rest of your filter, use .test[] .如果要提取该数组的各个元素以在过滤器的 rest 中使用,请使用.test[] If you want to process them but keep the results in an array, use .test|map(...) where ... is the filter you want to apply to the elements of the array.如果要处理它们但将结果保存在数组中,请使用.test|map(...)其中...是要应用于数组元素的过滤器。

How to filter objects with multiple conditions如何过滤具有多个条件的对象

As described in this question you can use the select() filter with multiple conditions.本问题所述,您可以将select()过滤器与多个条件一起使用。 So, for example:因此,例如:

jq '.test[]|
    select(
        (.outdated or .deprecated) and
        .release != "never-update-app" and
        .release != "never-upgrade-app"
    )'

How to combine non-trivial filters如何组合非平凡过滤器

Use parentheses, As an example, suppose you want to replace the two "never-" tests with a startswith test.使用括号,例如,假设您想用startswith测试替换两个“never-”测试。 By surrounding the pipes with parentheses you isolated the calculation and just get the true/false answer to combine in a boolean expression.通过用括号包围管道,您可以隔离计算并获得真/假答案以组合在 boolean 表达式中。

jq '.test[]|
    select(
        (.outdated or .deprecated) and
        (.release|startswith("never-")|not)
    )'

How to format results in a table如何在表格中格式化结果

I think this answer on formatting results in a table is probably sufficient.我认为这个关于在表格中格式化结果的答案可能就足够了。 If you have more specific needs I think you might want to spin this off into a separate question.如果您有更具体的需求,我认为您可能希望将其拆分为一个单独的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM