jq 内部数组值作为数组外的新键

Posted

技术标签:

【中文标题】jq 内部数组值作为数组外的新键【英文标题】:jq inner array value as new key outside the array 【发布时间】:2018-09-02 10:46:13 【问题描述】:

输入:


"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") | . += "domain":"sourcing"

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

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

【问题讨论】:

你需要在.data.assets[]中指定位置。 给定的输出和规定的目标不匹配。 "domain": "supply-chain"哪里来的??? 【参考方案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 )

将“域”设置为所有标签值

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

【讨论】:

这正是我要找的! 100% 达到了我的目的。太感谢了。但是我无法从 bash 脚本中运行这个命令?哪里不对了? echo $response | jq .data.assets |= map( if any(.tags[].value; . == "sourcing") then . + "domain":"sourcing" else . end ) 您必须添加合适的引号(例如 echo “$response” | jq ‘...’),或使用 -f 命令行选项。详见jq手册。 之前是我的错。我更正了引号,它的作用就像魅力一样。点赞!我有大约 6 个elif 条件。为避免这种情况,是否可以像arr["marketing","supply-chain","sourcing"]那样声明数组并比较并获得相同的输出? 是的,一旦你掌握了 jq 的基础知识,这是可能的,而且很容易做到。同时,您表示打算投票...... 我投了赞成票。他们说现在看不到!

以上是关于jq 内部数组值作为数组外的新键的主要内容,如果未能解决你的问题,请参考以下文章

如何使用jq根据内部数组中的值过滤对象数组?

jq json根据条件从内部数组中检索值并将其添加到数组之外

iOS plist中的新数组用新键覆盖旧数组用不同的键

jq 过滤内部数组元素但返回整个 JSON

展平 3D NumPy 数组中的内部元组并作为浮点数保存到 CSV

重温基础算法内部排序之计数排序法