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

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

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

问题

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

假设我有以下JSON:

  1. [
  2. {
  3. "name": "foo",
  4. "env": "dev",
  5. "version": "1.24"
  6. },
  7. {
  8. "name": "bar",
  9. "env": "staging",
  10. "version": "1.21"
  11. },
  12. {
  13. "name": "boo",
  14. "env": "prod",
  15. "version": "1.23"
  16. },
  17. {
  18. "name": "far",
  19. "env": "prod",
  20. "version": "1.24"
  21. }
  22. ]

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

我当前的命令是:

  1. 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:

  1. [
  2. {
  3. "name": "foo",
  4. "env": "dev",
  5. "version": "1.24"
  6. },
  7. {
  8. "name": "bar",
  9. "env": "staging",
  10. "version": "1.21"
  11. },
  12. {
  13. "name": "boo",
  14. "env": "prod",
  15. "version": "1.23"
  16. },
  17. {
  18. "name": "far",
  19. "env": "prod",
  20. "version": "1.24"
  21. }
  22. ]

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

My current command is:

  1. 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

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

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

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

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

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

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

  1. 输出:

"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:

  1. &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:

  1. &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:

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

Output:

  1. &quot;name&quot;,&quot;version&quot;
  2. &quot;foo&quot;,&quot;1.24&quot;
  3. &quot;bar&quot;,&quot;1.21&quot;
  4. &quot;boo&quot;,&quot;1.23&quot;
  5. &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:

确定