Using keys from the json src doc in a multiselect hash with Jmespath Using keys from the json src doc in a multiselect hash with Jmespath json json

Using keys from the json src doc in a multiselect hash with Jmespath


With only JMESPath you can use this query:

@.nics | {vlan_internal: @.vlan_internal | values(@)[0], vlan_external: @.vlan_external | values(@)[0]}

with this your source JSON you will get:

{  "vlan_internal": "aa:aa:aa:aa:aa:aa",  "vlan_external": "aa:aa:aa:aa:aa:bb"}


First of all im not sure you should go through all that troble.If you are uncertain witch keys you are goin to get, than you probebly would iterate through your entire json, and so, you know your item key.

Nevertheless, if you just want to export a new json, I was unable to find a way to use native JMESPath to do so. instead I used an ansible with_dict loop as follow:

     - name: Create new nic_dict json       set_fact:          nic_dict: "{{ {item.key: item.value.mac} | combine(nic_dict | default({})) }}"       with_dict: "{{ nics }}"

Now you can use the nic_dict that looks like this:

"nic_dict": {    "vlan_external": "aa:aa:aa:aa:aa:bb",    "vlan_internal": "aa:aa:aa:aa:aa:aa"}