根据值从字典列表中提取整个字典

根据值从字典列表中提取整个字典

我需要根据一个值从字典列表中提取整个字典。

"msg": [
    {
        "duplicate_key": "in dict_name_one",
        "KEY_1_dict_name_one": "VALUE_1_dict_name_one",
        "KEY_2_could_be anything": "VALUE_2_dict_name_one",
        "dict_name": "dict_name_one"
    },
    {
        "duplicate_key": "in dict_name_two",
        "KEY_1_second_dict": "VALUE_1_second_dict",
        "KEY_2_d_two": "VALUE_2_could_be anything",
        "dict_name": "dict_name_two"
    }
]

我想匹配“dict_name”的值(当 dict_name == dict_name_two 时)并仅获取包含该匹配的字典。

{
    "duplicate_key": "in dict_name_two",
    "KEY_1_second_dict": "VALUE_1_second_dict",
    "KEY_2_d_two": "VALUE_2_could_be anything",
    "dict_name": "dict_name_two"
}

这里唯一不变的是键“dict_name”。

我已经在 dict_name 上有一个循环和一个 rejectionattr 来根据剩余的键/值对设置一堆事实。

答案1

给出列表

  l0:
    - KEY_1_dict_name_one: VALUE_1_dict_name_one
      KEY_2_could_be anything: VALUE_2_dict_name_one
      dict_name: dict_name_one
      duplicate_key: in dict_name_one
    - KEY_1_second_dict: VALUE_1_second_dict
      KEY_2_d_two: VALUE_2_could_be anything
      dict_name: dict_name_two
      duplicate_key: in dict_name_two
    - KEY_1_second_dict: VALUE_1_second_dict
      KEY_2_d_two: VALUE_2_could_be anything
      duplicate_key: in dict_name_two

删除属性字典名称不见了

  l1: "{{ l0|rejectattr('dict_name', 'undefined') }}"

给出

  l1:
    - KEY_1_dict_name_one: VALUE_1_dict_name_one
      KEY_2_could_be anything: VALUE_2_dict_name_one
      dict_name: dict_name_one
      duplicate_key: in dict_name_one
    - KEY_1_second_dict: VALUE_1_second_dict
      KEY_2_d_two: VALUE_2_could_be anything
      dict_name: dict_name_two
      duplicate_key: in dict_name_two

还有更多选项。如果属性的值字典名称是唯一的创建字典

  d1: "{{ dict(l1|map(attribute='dict_name')|zip(l1)) }}"

给出

  d1:
    dict_name_one:
      KEY_1_dict_name_one: VALUE_1_dict_name_one
      KEY_2_could_be anything: VALUE_2_dict_name_one
      dict_name: dict_name_one
      duplicate_key: in dict_name_one
    dict_name_two:
      KEY_1_second_dict: VALUE_1_second_dict
      KEY_2_d_two: VALUE_2_could_be anything
      dict_name: dict_name_two
      duplicate_key: in dict_name_two

现在搜索已经变得很简单了。


完整测试剧本的示例

- hosts: localhost

  vars:

    l1:
      - KEY_1_dict_name_one: VALUE_1_dict_name_one
        KEY_2_could_be anything: VALUE_2_dict_name_one
        dict_name: dict_name_one
        duplicate_key: in dict_name_one
      - KEY_1_second_dict: VALUE_1_second_dict
        KEY_2_d_two: VALUE_2_could_be anything
        dict_name: dict_name_two
        duplicate_key: in dict_name_two

    d1: "{{ dict(l1|map(attribute='dict_name')|zip(l1)) }}"

  tasks:

    - debug:
        var: l1
    - debug:
        var: d1

下一个选项是过滤器选择属性或者json_查询

    - debug:
        msg: "{{ l1|selectattr('dict_name', 'eq', 'dict_name_two') }}"

    - debug:
        msg: "{{ l1|json_query('[?dict_name == `dict_name_two`]') }}"

这两个选项都提供了所选词典的列表,因为可能有更多项目具有相同的属性值字典名称

  msg:
  - KEY_1_second_dict: VALUE_1_second_dict
    KEY_2_d_two: VALUE_2_could_be anything
    dict_name: dict_name_two
    duplicate_key: in dict_name_two

如果你想替换一个变量,下面的表达式会给出相同的结果

    - debug:
        msg: "{{ l1|selectattr('dict_name', 'eq', dict_name) }}"
      vars:
        dict_name: dict_name_two

    - debug:
        msg: "{{ l1|json_query(_query) }}"
      vars:
        dict_name: dict_name_two
        _query: '[?dict_name == `{{ dict_name }}`]'

完整测试剧本的示例

 hosts: localhost

  vars:

    l1:
      - KEY_1_dict_name_one: VALUE_1_dict_name_one
        KEY_2_could_be anything: VALUE_2_dict_name_one
        dict_name: dict_name_one
        duplicate_key: in dict_name_one
      - KEY_1_second_dict: VALUE_1_second_dict
        KEY_2_d_two: VALUE_2_could_be anything
        dict_name: dict_name_two
        duplicate_key: in dict_name_two

  tasks:

    - debug:
        msg: "{{ l1|selectattr('dict_name', 'eq', 'dict_name_two') }}"
    - debug:
        msg: "{{ l1|json_query('[?dict_name == `dict_name_two`]') }}"

    - debug:
        msg: "{{ l1|selectattr('dict_name', 'eq', dict_name) }}"
      vars:
        dict_name: dict_name_two
    - debug:
        msg: "{{ l1|json_query(_query) }}"
      vars:
        dict_name: dict_name_two
        _query: '[?dict_name == `{{ dict_name }}`]'

相关内容