如何使用等效的通配符或jq中的替换来过滤多个对象标识符?

时间:2017-05-06 16:38:47

标签: json wildcard jq alternation

摘要

我有一个正常工作的jq过滤器,可以正确解析三个不同的名称有效负载对象,并按照所需的输出格式对其进行按摩。问题是我必须明确表达每个对象路径,因为我无法找到在对象标识符中表达替换的方法。

我希望过滤器更灵活,以便它可以显示从containers到任何包管理器的数据结构,标题以Packages开头。但是,我需要整个结构,而不仅仅是终端节点。

我认为我需要的是用交替或通配符表示我的对象标识符,例如:

  • .capabilities.*.payload?
  • .capabilities.([apk, dpkg, rpm]).payload?

我发现以上都不是有效的jq语法,这就是问题的原因。我在下面包含了一个带有有效JSON的测试语料库,我当前的jq过滤器列在下面的部分中。

最小JSON文件

这是我的测试语料库,在当前目录中存储为 minimal.json

{
  "containers": {
    "3dc76c82e566a116e5b64bc91a0b6220c71db7052f68317ebbe90521db55bf36": {
      "container_name": "/apache-46869",
      "capabilities": {
        "apk": {
          "title": "Packages (APK)"
        },
        "dpkg": {
          "title": "Packages (DPKG)",
          "payload": {
            "apt": "1.0.9.8.4",
            "libnghttp2-14": "1.18.1-1"
          }
        },
        "rpm": {
          "title": "Packages (RPM)"
        }
      }
    },
    "474047a1fe238e39fa1917aff0c93154624bbf159d321d49d5e685302589ab51": {
      "container_name": "/nginx-alpine-46869",
      "capabilities": {
        "apk": {
          "title": "Packages (APK)",
          "payload": {
            ".nginx-rundeps": "0",
            "apk-tools": "2.6.8-r2"
          }
        },
        "dpkg": {
          "title": "Packages (DPKG)"
        },
        "rpm": {
          "title": "Packages (RPM)"
        }
      }
    },
    "d7dcd90791240d78022941cf054a6b474f5329acd79aa15b58dc342f95a8ce33": {
      "container_name": "/apache-alpine-46869",
      "capabilities": {
        "apk": {
          "title": "Packages (APK)",
          "payload": {
            ".httpd-rundeps": "0",
            "apk-tools": "2.6.8-r2",
            "apr": "1.5.2-r1",
            "apr-util": "1.5.4-r2"
          }
        },
        "dpkg": {
          "title": "Packages (DPKG)"
        },
        "rpm": {
          "title": "Packages (RPM)"
        }
      }
    }
  }
}

显式jq过滤器

这是我当前的过滤器,它可以正常定义每个optional object indentifier-index

jq '
    [ .containers[] | { 
        name: .container_name, package_inventory: {
            apk: .capabilities.apk.payload?,
            dpkg: .capabilities.dpkg.payload?,
            rpm: .capabilities.rpm.payload?
        }   
    }]  
' minimal.json

预期产出

我当前的输出(如下所示)是正确的。目标不是修复输出,而是使过滤器更灵活。

[
  {
    "name": "/apache-46869",
    "package_inventory": {
      "apk": null,
      "dpkg": {
        "apt": "1.0.9.8.4",
        "libnghttp2-14": "1.18.1-1"
      },
      "rpm": null
    }
  },
  {
    "name": "/nginx-alpine-46869",
    "package_inventory": {
      "apk": {
        ".nginx-rundeps": "0",
        "apk-tools": "2.6.8-r2"
      },
      "dpkg": null,
      "rpm": null
    }
  },
  {
    "name": "/apache-alpine-46869",
    "package_inventory": {
      "apk": {
        ".httpd-rundeps": "0",
        "apk-tools": "2.6.8-r2",
        "apr": "1.5.2-r1",
        "apr-util": "1.5.4-r2"
      },
      "dpkg": null,
      "rpm": null
    }
  }
]

2 个答案:

答案 0 :(得分:1)

诀窍是定义辅助函数。例如,如果你写:

def payloads(keys): . as $in
  | reduce keys[] as $key ({}; .[$key] = ($in|.[$key].payload?) );

然后您的查询变为:

.containers[] | { 
    name: .container_name,
    package_inventory: (.capabilities | payloads( ["apk","dpkg","rpm"] ))
}

其他变体当然也是可能的。例如,您可以将payloads定义为arity-2函数,从而传递“功能”。

使用JSON对象指定键

以下是payloads/1的变体,说明了(a)如何避免reduce,以及(b)如何通过将JSON对象作为模板来指定密钥:

def payloads_at(object):
  . as $in
  | object as $object
  | ({}
     | [($object|keys_unsorted[]) as $key
        | .[$key] = ($in|.[$key].payload?) ])
  | add;

这可以这样调用:payloads_at({apk,dpkg,rpm}),或者如果你想要动态确定密钥:

(.capabilities | payloads_at( . ) )

答案 1 :(得分:0)

这个辅助函数可能更接近你想要的东西:

def star(pre; template; post):
  pre as $object
  | ({} | [($object|template|keys_unsorted[]) as $key | .[$key] = ($object | .[$key] | post) ])
  | add;

用法

明确的键名列表:

star(.capabilities; {apk,dpkg,rpm}; .payload)

.capabilities的键:

star(.capabilities; .; .payload)

实施例

.containers[] | { 
    name: .container_name,
    package_inventory: star(.capabilities; .; .payload)
}