减少字典列表中键的数量

huangapple go评论72阅读模式
英文:

Reducing the number of keys in a list of dictionaries

问题

"json": {
    "result_count": 4,
    "results": [
        {
            "_last_sync_time": 1678118608741,
            "display_name": "MyServerName1"
        },
        {
            "_last_sync_time": 1678118609865,
            "display_name": "MyServerName2"
        }
    ]
}
英文:

I have a list of 4 dictionaries with more data that I need, I am trying to display only 2 keys ("_last_sync_time" and "display_name") from the dictionaries but am unable to.

'json' is a dictionary and 'results' is the list of dictionaries that I need to grab only selected data from. Here is an example of my original data (there are 4 elements in the results list in real life).

"json": {
    "result_count": 4,
    "results": [
        {
            "_last_sync_time": 1678118608741,
            "cm_local_id": "host-29",
            "display_name": "MyServerName1",
            "external_id": "xxxxxxxx-5c6f-41f7-b9a1-783e45xxxxxx:host-29",
            "hardware_id": "xxxxxxa4e601d71c4a51a56cf5xxxxxx",
            "ip_addresses": [
                "10.x.x.1",
                "10.x.x.2"
            ],
            "node_type": "HostNode",
            "origin_id": "xxxxxxxx-5c6f-41f7-b9a1-783e45xxxxxx",
            "os_type": "ESXI",
            "os_version": "7.0.3",
            "resource_type": "DiscoveredNode",
            "stateless": false
        },
        {
            "_last_sync_time": 1678118609865,
            "cm_local_id": "host-30",
            "display_name": "MyServerName2",
            "external_id": "xxxxxxxx-6d7a-52a8-c0b2-894f56xxxxxx:host-30",
            "hardware_id": "xxxxxxa5f712e82d5b62b67da6xxxxxx",
            "ip_addresses": [
                "10.x.x.3",
                "10.x.x.4"
            ],
            "node_type": "HostNode",
            "origin_id": "xxxxxxxx-6d7a-52a8-c0b2-894f56xxxxxx",
            "os_type": "ESXI",
            "os_version": "7.0.3",
            "resource_type": "DiscoveredNode",
            "stateless": false
        }
    ]
}

I have tried using loop with map and selectattr but failed to get what I need i.e. the below

"json": {
    "result_count": 4,
    "results": [
        {
            "_last_sync_time": 1678118608741,
            "display_name": "MyServerName1"
        },
        {
            "_last_sync_time": 1678118609865,
            "display_name": "MyServerName2"
        }
    ]
}

答案1

得分: 0

- name: 尝试满足 OP 的要求
  vars:
    vars:
      query: >-
        {
          "result_count": result_count, 
          "results": results[].{
            "_last_sync_time": _last_sync_time, 
            "display_name": display_name
          }
        }
      ansible.builtin.debug:
        var: json | json_query(query)
英文:

Here's one way to do it with json_query:

- name: try to fulfill OP requirement
  vars:
    vars:
      query: >-
        {
          "result_count": result_count, 
          "results": results[].{
            "_last_sync_time": _last_sync_time, 
            "display_name": display_name
          }
        }
      ansible.builtin.debug:
        var: json | json_query(query)

huangapple
  • 本文由 发表于 2023年3月7日 01:43:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定