how to use json_query filter to extract all items equals to a value

Viewed 898

Here is my json output:

{
"kind": [
    {
        "inventory": "",
        "inventory_sources": "",
        "job_templates": "",
        "workflow_job_templates": "104"
    },
    {
        "inventory": "",
        "inventory_sources": "",
        "job_templates": "114",
        "workflow_job_templates": ""
    },
    {
        "inventory": "24",
        "inventory_sources": "",
        "job_templates": "",
        "workflow_job_templates": ""
    },
    {
        "inventory": "",
        "inventory_sources": "108",
        "job_templates": "",
        "workflow_job_templates": ""
    }
]

}

I'd like to display all items name that contain a specific value. For instance, I'd like to display that the item name taht contain '104' is 'workflow_job_templates'

I tested some syntaxes without any success:

    - debug: 
      msg: "104 is {{kind|json_query(query)}}"
      vars:
        query: "[?*==`104`].workflow_job_templates"

I know it is a wrong way to do but can someone tell me how he'd do for himself?

4 Answers

(Update)

The task below

    - debug:
        msg: "{{ item }} {{ kind|
                            map('dict2items')|
                            map('json_query', query)|
                            flatten }}"
      loop: [104, 114, 108, 24]
      vars:
        query: "[?to_string(value) == to_string('{{ item }}')].key"

gives

  msg: 104 ['workflow_job_templates']
  msg: 114 ['job_templates']
  msg: 108 ['inventory_sources']
  msg: 24 ['inventory']

(For the record. Brute-force approach)

Create a unique list of the keys

    - set_fact:
        my_keys: "{{ my_keys|default([]) + item.keys()|list }}"
      loop: "{{ kind }}"
    - set_fact:
        my_keys: "{{ my_keys|unique }}"

gives

  my_keys:
  - inventory
  - inventory_sources
  - job_templates
  - workflow_job_templates

Create a dictionary with all values

    - set_fact:
        my_dict: "{{ my_dict|default({})|combine({item: values}) }}"
      loop: "{{ my_keys }}"
      vars:
        query: "[].{{ item }}"
        values: "{{ kind|json_query(query) }}"

gives

  my_dict:
    inventory:
    - ''
    - ''
    - '24'
    - ''
    inventory_sources:
    - ''
    - ''
    - ''
    - '108'
    job_templates:
    - ''
    - '114'
    - ''
    - ''
    workflow_job_templates:
    - '104'
    - ''
    - ''
    - ''

Then search the dictionary. For example

    - debug:
        msg: "{{ item }} {{ my_dict|dict2items|json_query(query) }}"
      loop: [104, 114, 108, 24]
      vars:
        query: "[?value[?contains(@, '{{ item }}')]].key"

gives

  msg: 104 ['workflow_job_templates']
  msg: 114 ['job_templates']
  msg: 108 ['inventory_sources']
  msg: 24 ['inventory']

json_query could be part of the equation for your solution but is really not needed here.

Explanation of the below piece of code:

  • Apply the dict2items filter to each element of your list. This transforms each mapping to a list of {key: "key", value: "value"} pairs
  • Flatten the given list so we get all those elements to a single top level
  • Select elements having a value of '104' only
  • Extract the key attribute of each element in a list
  • Make that list unique and sort it.
    - name: Display all element having a value of 104
      debug:
        msg: "{{ kind | map('dict2items') | flatten | selectattr('value', '==', '104') | map(attribute='key') | unique | sort }}"

Please note that this solution will give you a result if the same key name as different values but one of them is '104. With your above data the result is:

TASK [Display all element having a value of 104] ***************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "workflow_job_templates"
    ]
}

If you really just want to use json_query()

---
- name: PLAYBOOK Filtering
  hosts: localhost # run locally
  
  tasks:
  - name: Create json
    set_fact:
      kind: '{{ lookup("file", "kind.json") }}'

  - name: check the var was created properly
    debug:
      var: kind 

  - name: output the element that matches 104
    debug: 
      msg: "{{ kind | json_query(\"kind[?workflow_job_templates=='104'].workflow_job_templates\") }}"

  - name: 
    set_fact: 
      output: "{{ kind | json_query(\"kind[?workflow_job_templates=='104'].workflow_job_templates\") }}"

Output

TASK [output the element that matches 104] *************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "104"
    ]
}

The correct alternative of selectattr with json_query is:

    - debug:
        msg: "{{ kind | map('dict2items') | flatten | json_query(query)}}"
      vars:
        - query: "[?value == `\"104\"`].key"
Related