使用jq递归提取对象值和父键名称 [英] Recursive extraction of object values and parent key name using jq
问题描述
我需要解析npm ls --global --json
命令的输出,以便获得以下格式的所有已安装npm
软件包的列表:
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:
$package;$version;js;$resolved
位置:
-
$package
是包含每个dependencies
对象中软件包名称的键. -
$version
是每个包装中的version
值 -
js
只是一个文字字符串 -
$resolved
是从每个软件包中获取的resolved
值
$package
is the key containing the package name, from eachdependencies
object.$version
is theversion
value taken from each packagejs
is just a literal string$resolved
is theresolved
value taken from each package
我已经了解了以下命令语法和输出:
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:
-
如何在包含该程序包名称的
.dependencies
中获取要迭代的键名值.似乎到那时我正在查看该对象本身的内容.
How can I get the key name value that I am iterating over in
.dependencies
that contains that package name. It seems that by that point I am looking at the contents of that object itself.
如何遍历所有依赖项对象?目前,我仅查看根.dependencies
对象中的顶级记录.我发现了..
递归,但是我不太确定如何在这里应用它.
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. 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
我在上面的示例中使用的一些(大大减少的)样本输出npm ls --global --json
如下:
Some (much reduced) sample output npm ls --global --json
that I have used for the above example, is as follows:
{
"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"
}
}
}
}
}
}
}
推荐答案
通过使用..
,它将遍历json树中的所有值.因此,您将希望通过具有所需结构的对象来过滤掉这些对象.在这种情况下,具有有效dependencies
对象的事物.找到对象后,可以提取所需的值.
By using ..
, it will recurse through all values in the json tree. 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. 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
产生结果:
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
这篇关于使用jq递归提取对象值和父键名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!