简体   繁体   English

jq数组过滤从一个文件到另一个文件

[英]jq array filtration from a file to another file

I have the below json in which i want to filter out the result object which is an Array using some script, since result object can populate multiple objects in it.我有下面的 json ,我想在其中过滤掉结果object 这是一个使用一些脚本的数组,因为结果 object 可以在其中填充多个对象。 I wanted to get the data from " jq -c.results[i]."com.src.main.client.details" into a json file to process further.我想将“ jq -c.results[i]."com.src.main.client.details"中的数据放入 json 文件中以进一步处理。

{
    "foo": {
        "requestID": "89279f54-2f18-4301-b94d-1c413be1cb68",
        "signature": {
            "*": "*"
        }
    },
    "results": [
        {
            "com.src.main.client.details": {
                "doc": "string",
                "details": [
                    {
                        "amount": null,
                        "output": null,
                        "properties": [],
                        "characteristic": [],
                        "component": null,
                        "period": null,
                        "internals": {
                            "Currency": "EUR",
                           "value": 0
                        }
                    }
                ]
            }
        },
        {
            "com.src.main.client.details": {
                "doc": "string",
                "details": [
                    {
                        "amount": null,
                        "output": null,
                        "properties": [
                            {
                                "characteristic": [],
                                "component": null,
                                "period": null,
                                "internals": {
                                    "Currency": "EUR",
                                    "value": 0
                                }
                            }
                        ]
                    }
                ]
            }
        }
    ]
}

Is there a way I can achieve it via one single command or if someone can suggest the scripting logic.有没有一种方法可以通过一个命令实现它,或者如果有人可以建议脚本逻辑。 Thanks.谢谢。

Desire Output:渴望 Output:

[
  {
    "doc": "string",
    "details": [
      {
        "amount": null,
        "output": null,
        "properties": [],
        "characteristic": [],
        "component": null,
        "period": null,
        "internals": {
          "Currency": "EUR",
          "value": 0
        }
      }
    ]
  },
  {
    "doc": "string",
    "details": [
      {
        "amount": null,
        "output": null,
        "properties": [
          {
            "characteristic": [],
            "component": null,
            "period": null,
            "internals": {
              "Currency": "EUR",
              "value": 0
            }
          }
        ]
      }
    ]
  }
]

Three issues.三个问题。


.results[i] should be .results[] .results[i]应该是.results[]


The following produces a stream of JSON objects:以下生成 JSON 对象的 stream :

.results[]."com.src.main.client.details"

To get an array, use要获取数组,请使用

[ .results[]."com.src.main.client.details" ]

or或者

.results | map(."com.src.main.client.details")

Finally, there was a shell quoting issue.最后,出现了 shell 引用问题。 You want你要

jq -c '.results | map(."com.src.main.client.details")'

Note the single quotes.注意单引号。

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

相关问题 使用“jq”从另一个 json 文件附加到 json 文件中的数组 - Append to array in json file from another json file with “jq” JQ:将数组与另一个文件中的对象列表组合 - JQ: Combine array with a list of objects from another file Jq:将一个文件中的对象附加到另一个文件中 - Jq: appending an object from 1 file into another file 在jq中从另一个减去一个json文件 - subtracting one json file from another in jq jq 替换文件中的数组键值 - jq to replace array key values from file 使用 jq 将文件中的数组元素添加到其他文件的 json 数组中 - add array element from file into json array of other file with jq 使用jq将json对象从一个文件添加到单个数组下的另一个文件 - adding json objects from one file to another under single array using jq 如果键与使用 jq 的同一数组中的另一个键匹配,则从 JSON 文件中删除条目 - Removing entry from JSON file if the key matches another key in the same array using jq jq:将数组中的值与常量相乘,然后将结果添加到另一个文件中具有匹配ID的对象中 - jq: Multiply values from array with constants and add result to objects with matching id in another file 将 Bash 文件中的变量通过管道传输到另一个 Bash 文件中的 JQ 命令 - Pipe variable from a Bash file into a JQ command in another Bash file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM