0

I want to filter every keys value from below json using json_query | JMESPATH, how to achieve this?

{
  "facts_hash": {
    "processors::count": "96",
    "processors::physicalcount": "2",
    "processor0": "Intel(R) Xeon(R) Gold 6252 CPU @ 2.10GHz",
    "processorcount": "96",
    "macaddress": "08:f1:ea:6d:04:3G",
    "ipaddress": "192.168.101.135",
    "manufacturer": "HPE",
    "productname": "ProLiant DL360 Gen10",
    "serialnumber": "SGH93GDCR",
    "memorysize_mb": "773730.12",
    "ipmi_ipaddress": "172.16.200.28",
      },
  "id": 284
}

Expected output, can be as below

  [ "96",
    "2",
    "Intel(R) Xeon(R) Gold 6252 CPU @ 2.10GHz",
    "96",
    "08:f1:ea:6d:03:3c",
    "192.168.101.135",
    "HPE",
    "ProLiant DL360 Gen10",
    "SGH936XG91",
    "773730.12",
    "172.16.100.24",
    "284" ]

Here is the below query which i have tried which return the value.

"{{ facts_hash | json_query('[manufacturer, ipaddress, macaddress, productname, serialnumber, processorcount, processor0, memorysize_mb, ipmi_ipaddress]') | list }}"

few keys are in json with four :: which I am not able to filter here are those keys "processors::count" & "processors::physicalcount" along with i am not able to fetch the "id"

query return value.

 ok: [localhost] => {
    "ansible_facts": {
        "allvalue": [
            "HPE",
            "192.168.101.135",
            "08:f1:ea:6d:03:3c",
            "ProLiant DL360 Gen10",
            "SGH936XG91",
            "96",
            "Intel(R) Xeon(R) Gold 6252 CPU @ 2.10GHz",
            "773730.12",
            "172.16.100.24"
        ]
    },
    "changed": false
}

Thanks

Bharat Gupta
  • 127
  • 1
  • 10
  • You will want to [edit your question](https://stackoverflow.com/posts/62044222/edit) and include what you have tried so far that is not working for you, including any relevant error message, too – mdaniel May 27 '20 at 15:54
  • whatever i have tried mentioned in the question based on your ask. please check. – Bharat Gupta May 27 '20 at 16:35
  • if i add the four dot key such as processors::count then the is the error i get ------------------------------------------fatal: [localhost]: FAILED! => { "msg": "JMESPathError in json_query filter plugin:\nExpecting: comma, got: colon: Parse error at column 25, token \":\" (COLON), for expression:\n\"[manufacturer, processors::count, ipaddress, macaddress, productname, serialnumber, processorcount, processor0, memorysize_mb, ipmi_ipaddress]\"\n ^" } – Bharat Gupta May 27 '20 at 16:45

1 Answers1

0

I would do that with facts_hash | dict2items | map(attribute="value") | list myself, but the answer to your question appears to be to quote the key names in the JMESPath selector:

- debug:
    msg: "{{ facts_hash |
      json_query('[\"processors::count\",  \"processors::physicalcount\", manufacturer, ipaddress, macaddress, productname, serialnumber, processorcount, processor0, memorysize_mb, ipmi_ipaddress]') | list }}"

which one can make a little more legible by extracting the JMESPath expression out to its own local variable:

- debug:
    msg: "{{ facts_hash | json_query(facts_query) | list }}"
  vars:
    facts_query: '["processors::count", "processors::physicalcount", manufacturer, ipaddress, macaddress, productname, serialnumber, processorcount, processor0, memorysize_mb, ipmi_ipaddress]'

as for

along with i am not able to fetch the "id"

that's because id is a sibling of facts_hash, so whatever structure contains facts_hash that you are doing a set_fact: to extract facts_hash, you'll want a similar extraction to grab its id field

mdaniel
  • 31,240
  • 5
  • 55
  • 58