当键值不是数组时使用 ansible json_query 时获取 null

Getting null while using ansbile json_query when key value is not array

我有以下数据:

{
  "msg": [
    {
      "country": {
        "city": {
          "county": [
            {
              "zipcode": "code1",
              "address": "add1"
            },
            {
              "zipcode": "code2",
              "address": "add2"
            },
            {
              "zipcode": "code3",
              "address": "add3"
            }
          ]
        }
      },
      "countyname": "county-a"
    },
    {
      "country": {
        "city": {
          "county": {
            "zipcode": "code1",
            "address": "add1"
          }
        }
      },
      "countyname": "county-b"
    }
  ]
}

第一次尝试:

- name: data
  debug: out= "{{msg | json_query(query)}}"
  vars:
    query: "[].{county: countyname, zipcode: country.city.county[].zipcode , address: country.city.county[].address}"

结果:

[
  {
    "county": "county-a",
    "zipcode": [
      "code1",
      "code2",
      "code3"
    ],
    "address": [
      "add1",
      "add2",
      "add3"
    ]
  },
  {
    "Name": "county-b",
    "local": null,
    "remote": null
  }
]

第二次尝试:

- name: data
  debug: out= "{{msg | json_query(query)}}"
  vars:
    query: "[].{county: countyname, zipcode: country.city.county.zipcode , address: country.city.county.address}"

结果:

[
  {
    "county": "county-a",
    "zipcode": null,
    "address": null
  },
  {
    "county": "county-b",
    "zipcode": "code1",
    "address": "add1"
  }
]

如果我使用 query: "[].{County: countyname, zipcode: country.city.county[].zipcode, address: country.city.county[].address}",我会得到 county-bnull 个值。

如果我使用 query: "[].{County: countyname, zipcode: country.city.county.zipcode, address: country.city.county.address}",我会得到 county-anull 个值。

使用 json_query 的正确方法是什么,以便使用单个查询,我得到两个县的数据,如

[
  {
    "county": "county-a",
    "zipcode": [
      "code1",
      "code2",
      "code3"
    ],
    "address": [
      "add1",
      "add2",
      "add3"
    ]
  },
  {
    "Name": "county-b",
    "zipcode": "code1",
    "address": "add1"
  }
]

您可以使用 or expression (||),它将捕获 null 值并在第一个表达式确实为 null.

时显示第二个表达式

所以像这样:

zipcode: country.city.county.zipcode || country.city.county[].zipcode

因此,整个 JMESPath 查询最终为:

[].{county: countyname, zipcode: country.city.county.zipcode || country.city.county[].zipcode, address: country.city.county.address || country.city.county[].address}

鉴于任务:

- debug:
    var: msg | json_query(query)
  vars:
    query: >-
      [].{
        county: countyname, 
        zipcode: country.city.county.zipcode || country.city.county[].zipcode, 
        address: country.city.county.address || country.city.county[].address
      }
    msg: 
      - country:
          city:
            county:
              - zipcode: code1
                address: add1
              - zipcode: code2
                address: add2
              - zipcode: code3
                address: add3
        countyname: county-a
      - country:
          city:
            county:
              zipcode: code1
              address: add1
        countyname: county-b

这产生:

{
    "msg | json_query(query)": [
        {
            "address": [
                "add1",
                "add2",
                "add3"
            ],
            "county": "county-a",
            "zipcode": [
                "code1",
                "code2",
                "code3"
            ]
        },
        {
            "address": "add1",
            "county": "county-b",
            "zipcode": "code1"
        }
    ]
}