Jq从嵌套哈希获取输出

时间:2018-03-23 09:30:25

标签: jq

您好我正在尝试使用jq

解析哈希
{
    "name": "a",
    "data": [
    {
        "sensitive": false,
        "type": "string",
        "value": "mykeypair"
    },
    {
        "sensitive": false,
        "type": "int",
        "value": 123
    }
    ]
}

并获得类似

的输出
a,string,mykeypair
a,int,123

我能够得到像这样的输出

a,string,mykeypair
a,int,mykeypair
a,string,123
a,int,123

1 个答案:

答案 0 :(得分:3)

jq 解决方案:

jq -r '.name as $n | .data[] | [$n, .type, .value] | @csv' file.json

输出:

"a","string","mykeypair"
"a","int",123

如果必须输出不带引号的值:

jq -r '.name as $n | .data[] | [$n, .type, "\(.value)"] | join(",")' file.json

输出:

a,string,mykeypair
a,int,123