简体   繁体   English

如何使用 jq 1.5 删除散列数组 JSON 输入的 hash 中的最后一个“列”?

[英]How to remove last "column" in a hash of an array of array of hashes JSON input using jq 1.5?

I want to convert the following JSON content stored in a file tmp.json我想转换以下 JSON 内容存储在文件 tmp.json

{
    "results": [
        [
           {
               "field": "field1",
               "value": "value1-1"
           },
           {
               "field": "field2",
               "value": "value1-2"
           },
           {
               "field": "field3",
               "value": "value1-3"
           }
        ],
        [
           {
               "field": "field1",
               "value": "value2-1"
           },
           {
               "field": "field2",
               "value": "value2-2"
           },
           {
               "field": "field3",
               "value": "value2-3"
           }
        ],
        [
           {
               "field": "field1",
               "value": "value3-1"
           },
           {
               "field": "field2",
               "value": "value3-2"
           },
           {
               "field": "field3",
               "value": "value3-3"
           }
        ]
    ]
}

into CSV output:进入 CSV output:

"field1","field2"
"value1-1","value1-2"
"value2-1","value2-2"
"value3-1","value3-2"

The closest jq expression I've come up with is this:我想出的最接近的 jq 表达式是:

cat ./tmp.json | jq -r '.results | [ .[] | del(last) ] | (first | map(.field)), (.[] | map(.value)) | @csv'

It works for jq version 1.6, but for version 1.5, the last "column" is still included in the CSV result.它适用于 jq 1.6 版,但对于 1.5 版,最后一个“列”仍包含在 CSV 结果中。 How do I edit the jq code so that it works for version 1.5?如何编辑 jq 代码以使其适用于 1.5 版?

Note that the number of columns is not limited to 3;注意列数不限于3; it can be more.它可以更多。 The jq code should be able to remove the last column in the final CSV result. jq 代码应该能够删除最终 CSV 结果中的最后一列。

You can use the slice operator to extract the sub-elements of the array inside each field and put into an array and use the @csv您可以使用切片运算符提取每个字段内的数组子元素并放入数组中并使用@csv

[ .results[][:-1] ] | (first | map(.field)), (.[] | map(.value)) | @csv

The part .results[][:-1] extracts all the elements except the last one in the array. .results[][:-1]部分提取数组中除最后一个元素之外的所有元素。 From the manual从手册

Either index may be negative (in which case it counts backwards from the end of the array), or omitted (in which case it refers to the start or end of the array).任何一个索引都可以是负数(在这种情况下,它从数组的末尾向后计数),或者被省略(在这种情况下,它指的是数组的开头或结尾)。

See jqplay for a working demo.有关工作演示,请参阅jqplay


I was able to reproduce the bug in jq-1.5.我能够重现 jq-1.5 中的错误。 Its a known fact that there are major bugs fixed as part of release 1.6 in del/1 .众所周知的事实是,在 del/1 中修复了作为 1.6 版的一部分的主要错误。 So consider upgrading to use del/1 or use the slice expression as indicated in the answer above因此,请考虑升级以使用 del/1 或使用上面答案中指示的切片表达式

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

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