简体   繁体   English

使用jq递归提取对象值和父键名称

[英]Recursive extraction of object values and parent key name using jq

I have a requirement to parse the output of the npm ls --global --json command, such that I get a list of all the installed npm packages in the following format: 我需要解析npm ls --global --json命令的输出,以便获得以下格式的所有已安装npm软件包的列表:

$package;$version;js;$resolved

Where: 哪里:

  • $package is the key containing the package name, from each dependencies object. $package是包含每个dependencies对象中包名称的键。
  • $version is the version value taken from each package $version是从每个软件包中获取的version
  • js is just a literal string js只是一个文字字符串
  • $resolved is the resolved value taken from each package $resolved是从每个包中获取的resolved

I have gotten as far as this command syntax and output: 我已经了解了以下命令语法和输出:

$ jq --raw-output 'select( has("dependencies") ) .dependencies[] | . as $d | "parentkey" + ";" + $d.version + ";js;" + $d.resolved'`
parentkey;5.5.1;js;
parentkey;1.1.3;js;https://registry.npmjs.org/yaml-table/-/yaml-table-1.1.3.tgz

The parts that I am specificly having difficulty with are as follows: 我具体遇到的部分如下:

  • How can I get the key name value that I am iterating over in .dependencies that contains that package name. 如何在包含该包名称的.dependencies.dependencies要迭代的键名值。 It seems that by that point I am looking at the contents of that object itself. 似乎到那时我正在查看该对象本身的内容。

  • How can I recurse through ALL dependency objects? 如何遍历所有依赖项对象? At the moment I'm only looking at the top level records in the root .dependencies object. 目前,我仅查看根.dependencies对象中的顶级记录。 I've discovered .. recursion, but I'm not quite sure how to apply it here. 我发现了..递归,但是我不太确定如何在这里应用它。

Based on the example data below, I am trying to reach the following output results: 基于下面的示例数据,我试图达到以下输出结果:

npm;5.5.1;js;
JSONStream;1.3.1;js;https://registry.npmjs.org/JSONStream/-/JSONStream-1.3.1.tgz
jsonparse;1.3.1;js;https://registry.npmjs.org/jsonparse/-/jsonparse-1.3.1.tgz
through;2.3.8;js;https://registry.npmjs.org/through/-/through-2.3.8.tgz
yaml-table;1.1.3;js;https://registry.npmjs.org/yaml-table/-/yaml-table-1.1.3.tgz
js-yaml;3.4.6;js;https://registry.npmjs.org/js-yaml/-/js-yaml-3.4.6.tgz
argparse;1.0.9;js;https://registry.npmjs.org/argparse/-/argparse-1.0.9.tgz

Some (much reduced) sample output npm ls --global --json that I have used for the above example, is as follows: 我在上面的示例中使用的一些(大大减少了)样本输出npm ls --global --json如下:

{
  "dependencies": {
    "npm": {
      "version": "5.5.1",
      "dependencies": {
        "JSONStream": {
          "version": "1.3.1",
          "from": "JSONStream@~1.3.1",
          "resolved": "https://registry.npmjs.org/JSONStream/-/JSONStream-1.3.1.tgz",
          "dependencies": {
            "jsonparse": {
              "version": "1.3.1",
              "from": "jsonparse@^1.2.0",
              "resolved": "https://registry.npmjs.org/jsonparse/-/jsonparse-1.3.1.tgz"
            },
            "through": {
              "version": "2.3.8",
              "from": "through@>=2.2.7 <3",
              "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz"
            }
          }
        }
      }
    },
    "yaml-table": {
      "version": "1.1.3",
      "from": "yaml-table@latest",
      "resolved": "https://registry.npmjs.org/yaml-table/-/yaml-table-1.1.3.tgz",
      "dependencies": {
        "js-yaml": {
          "version": "3.4.6",
          "from": "js-yaml@3.4.6",
          "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.4.6.tgz",
          "dependencies": {
            "argparse": {
              "version": "1.0.9",
              "from": "argparse@>=1.0.2 <2.0.0",
              "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.9.tgz"
            }
          }
        }
      }
    }
  }
}

By using .. , it will recurse through all values in the json tree. 通过使用.. ,它将遍历json树中的所有值。 So you'll want to filter those out by objects that have the structure you're expecting. 因此,您将希望通过具有期望结构的对象来过滤掉这些对象。 In this case, things that have a valid dependencies object. 在这种情况下,具有有效dependencies事物。 Once you've located the objects, you could extract the values you want. 找到对象后,可以提取所需的值。

jq -r '.. | .dependencies? | objects
    | to_entries[] | [.key, .value.version, "js", .value.resolved] | join(";")' input.json

produces the results: 产生结果:

npm;5.5.1;js;
yaml-table;1.1.3;js;https://registry.npmjs.org/yaml-table/-/yaml-table-1.1.3.tgz
JSONStream;1.3.1;js;https://registry.npmjs.org/JSONStream/-/JSONStream-1.3.1.tgz
jsonparse;1.3.1;js;https://registry.npmjs.org/jsonparse/-/jsonparse-1.3.1.tgz
through;2.3.8;js;https://registry.npmjs.org/through/-/through-2.3.8.tgz
js-yaml;3.4.6;js;https://registry.npmjs.org/js-yaml/-/js-yaml-3.4.6.tgz
argparse;1.0.9;js;https://registry.npmjs.org/argparse/-/argparse-1.0.9.tgz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM