jq内部数组值作为数组

时间:2018-03-23 17:08:14

标签: arrays jq flatten

输入:

{
"data": {
    "assets": [{
            "organizationId": "1056bda9-2598-4fdf-bd99-db3924464a75",
            "createdAt": "2018-03-14T14:41:41.154Z",
            "tags": [{
                    "value": "raml",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "rest",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "api",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "v1",
                    "key": "product-api-version",
                    "mutable": false
                },
                {
                    "value": "has-mule4-connector",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "has-mule3-connector",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "system",
                    "key": null,
                    "mutable": true
                },
                {
                    "value": "sourcing",
                    "key": null,
                    "mutable": true
                }
            ],
            "type": "rest-api"
        },
        {
            "organizationId": "SASAAs",
            "createdAt": "2018-03-14T14:41:41.154Z",
            "tags": [{
                    "value": "raml",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "rest",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "api",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "v1",
                    "key": "product-api-version",
                    "mutable": false
                },
                {
                    "value": "has-mule4-connector",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "has-mule3-connector",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "system",
                    "key": null,
                    "mutable": true
                },
                {
                    "value": "supply-chain",
                    "key": null,
                    "mutable": true
                }
            ],
            "type": "rest-api"
        }   
    ]
}

}

预期产出:

{
"data": {
    "assets": [{
            "organizationId": "1056bda9-2598-4fdf-bd99-db3924464a75",
            "createdAt": "2018-03-14T14:41:41.154Z",
            "tags": [{
                    "value": "raml",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "rest",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "api",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "v1",
                    "key": "product-api-version",
                    "mutable": false
                },
                {
                    "value": "has-mule4-connector",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "has-mule3-connector",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "system",
                    "key": null,
                    "mutable": true
                },
                {
                    "value": "sourcing",
                    "key": null,
                    "mutable": true
                }
            ],
            "type": "rest-api",
            "domain": "sourcing"
        },
        {
            "organizationId": "SASAAs",
            "createdAt": "2018-03-14T14:41:41.154Z",
            "tags": [{
                    "value": "raml",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "rest",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "api",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "v1",
                    "key": "product-api-version",
                    "mutable": false
                },
                {
                    "value": "has-mule4-connector",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "has-mule3-connector",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "system",
                    "key": null,
                    "mutable": true
                },
                {
                    "value": "supply-chain",
                    "key": null,
                    "mutable": true
                }
            ],
            "type": "rest-api",
            "domain": "supply-chain"
        }   
    ]
}

}

到目前为止,我尝试过这部分对我有用。

.data.assets [] |选择(.tags [] .value ==“sourcing”)| 。 + = {“域名”:“采购”}

问题是我希望这个条件适用于数组中的每个对象,但我无法做到这一点。它仅应用于第一个对象。

我在哪里做错了?有什么建议吗?

1 个答案:

答案 0 :(得分:1)

以下似乎符合描述性要求:

.data.assets |=
  map( if any(.tags[].value; . == "sourcing")
       then . + {"domain":"sourcing"}
       else .
       end )

除了与描述性要求不一致的键值对"domain": "supply-chain"之外,这会产生所需的输出。

相反,以下内容从给定输出中获取(即产生)其提示:

.data.assets |=
  map( if any(.tags[].value; . == "sourcing") then . + {"domain":"sourcing"}
       elif any(.tags[].value; . == "supply-chain") then . + {"domain":"supply-chain"}
       else . end )

将“domain”设置为所有标记值

.data.assets |= map( .domain += [.tags[].value] )