如何使用jq将原始JSON中的特定字段筛选到CSV中?

huangapple go评论56阅读模式
英文:

How can I use jq to filter only certain fields of the original JSON into an CSV?

问题

我说此Stack Overflow答案使用简单的jq命令将JSON转换为CSV文件,但我需要进一步改进它。

假设我有以下JSON:

[
    {
        "name": "foo",
        "env": "dev",
        "version": "1.24"
    },
    {
        "name": "bar",
        "env": "staging",
        "version": "1.21"
    },
    {
        "name": "boo",
        "env": "prod",
        "version": "1.23"
    },
    {
        "name": "far",
        "env": "prod",
        "version": "1.24"
    }
]

如何只创建包含"name"和"version"字段的CSV?

我当前的命令是:

jq -r '(map(keys) | add | unique) as $cols | map(.[] | {name, version} as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv'

这不起作用。有人可以提供一些帮助吗?

谢谢!

英文:

I say this stack overflow anser with a simple jq command to convert a JSON to a csv file, but I need to improve it further.

Say I have the following JSON:

[
    {
        "name": "foo",
        "env": "dev",
        "version": "1.24"
    },
    {
        "name": "bar",
        "env": "staging",
        "version": "1.21"
    },
    {
        "name": "boo",
        "env": "prod",
        "version": "1.23"
    },
    {
        "name": "far",
        "env": "prod",
        "version": "1.24"
    }
]

How does one create the CSV with only the "name" and "version" fields?

My current command is:

jq -r '(map(keys) | add | unique) as $cols | map(.[] | {name, version} as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv'

This is not working. Can anyone provide some help?

Thanks!

答案1

得分: 4

如果你已经知道两个列名,你可以直接使用`.name`和`.version`来提取它们:

<file jq -r '[ "name", "version" ], ( .[] | [ .name, .version ] ) | @csv'


你也可以使用你的`$cols`变量,这样列名只会出现一次:

<file jq -r '[ "name", "version" ] as $cols | $cols, ( .[] | [ .[$cols[]] ] ) | @csv'


或者动态导入它们,例如使用`--args`:

<file jq -r '$ARGS.positional, ( .[] | [ .[$ARGS.positional[]] ] ) | @csv'
--args name version


输出:

"name","version"
"foo","1.24"
"bar","1.21"
"boo","1.23"
"far","1.24"

英文:

If you know the two column names anyway, you could simply extract them directly using .name and .version:

&lt;file jq -r &#39;[&quot;name&quot;, &quot;version&quot;], (.[] | [.name, .version]) | @csv&#39;

You can also use your $cols variable, so the names only appear once:

&lt;file jq -r &#39;[&quot;name&quot;, &quot;version&quot;] as $cols | $cols, (.[] | [.[$cols[]]]) | @csv&#39;

Or import them dynamically, e.g. using --args:

&lt;file jq -r &#39;$ARGS.positional, (.[] | [.[$ARGS.positional[]]]) | @csv&#39; \
  --args name version

Output:

&quot;name&quot;,&quot;version&quot;
&quot;foo&quot;,&quot;1.24&quot;
&quot;bar&quot;,&quot;1.21&quot;
&quot;boo&quot;,&quot;1.23&quot;
&quot;far&quot;,&quot;1.24&quot;

huangapple
  • 本文由 发表于 2023年2月17日 23:51:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75486489.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定