过滤嵌套对象中的部分

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

Filtering out sections in nested object

问题

以下是您要的翻译的内容:

  1. {
  2. "foo": {},
  3. "bar": {
  4. "a": {
  5. "tags": [
  6. "x",
  7. "y"
  8. ],
  9. "cycle": "simple"
  10. }
  11. },
  12. "qux": {
  13. "d": {
  14. "tags": [
  15. "x",
  16. "y",
  17. "z"
  18. ],
  19. "cycle": "complex"
  20. }
  21. }
  22. }
英文:

Given the following json:

  1. {
  2. "foo": {},
  3. "bar": {
  4. "a": {
  5. "tags": [
  6. "x",
  7. "y"
  8. ],
  9. "cycle": "simple"
  10. },
  11. "b": {
  12. "tags": [
  13. "x"
  14. ],
  15. "cycle": null
  16. }
  17. },
  18. "baz": {
  19. "c": {
  20. "tags": [
  21. "y"
  22. ],
  23. "cycle": null
  24. }
  25. },
  26. "qux": {
  27. "d": {
  28. "tags": [
  29. "x",
  30. "y",
  31. "z"
  32. ],
  33. "cycle": "complex"
  34. }
  35. }
  36. }

I'd like to drop all objects in the top-level object which have "cycle == null". When filtering for "cycle", I don't know how to access fields from earlier filters:

  1. $ jq '.[] | .[] | select(.cycle != null)' data.json
  2. {
  3. "tags": [
  4. "x",
  5. "y"
  6. ],
  7. "cycle": "simple"
  8. }
  9. {
  10. "tags": [
  11. "x",
  12. "y",
  13. "z"
  14. ],
  15. "cycle": "complex"
  16. }

I'd like to keep the structure of the matching entries, something like this:

  1. {
  2. "bar": {
  3. "a": {
  4. "tags": [
  5. "x",
  6. "y"
  7. ],
  8. "cycle": "simple"
  9. }
  10. },
  11. "qux": {
  12. "d": {
  13. "tags": [
  14. "x",
  15. "y",
  16. "z"
  17. ],
  18. "cycle": "complex"
  19. }
  20. }
  21. }

I was hoping to write something like this jq '.*.*. | select(.cycle != null)' so that I wouldn't need to manually re-assemble the original structure, but didn't understand wildcards.

答案1

得分: 1

.[] |= …(或 map_values(…))允许您操作对象的内容,而不会丢失外部结构。嵌套使用两次可以深入到两个级别。select(.cycle != null) 是您的主要筛选条件,但我还添加了 select(. != {}) 在中间级别,因为您的期望输出不包括 "foo": {}

  1. jq '.[] |= (.[] |= select(.cycle != null) | select(. != {}))'
  1. {
  2. "bar": {
  3. "a": {
  4. "tags": [
  5. "x",
  6. "y"
  7. ],
  8. "cycle": "simple"
  9. }
  10. },
  11. "qux": {
  12. "d": {
  13. "tags": [
  14. "x",
  15. "y",
  16. "z"
  17. ],
  18. "cycle": "complex"
  19. }
  20. }
  21. }

演示

英文:

.[] |= … (or map_values(…)) lets you manipulate the contents of objects without losing the outer structure. Nest it twice to go two levels deep. select(.cycle != null) is your main filter, but I've also added select(. != {}) on the intermediate level as your desired output didn't include "foo": {}.

  1. jq '.[] |= (.[] |= select(.cycle != null) | select(. != {}))'
  1. {
  2. "bar": {
  3. "a": {
  4. "tags": [
  5. "x",
  6. "y"
  7. ],
  8. "cycle": "simple"
  9. }
  10. },
  11. "qux": {
  12. "d": {
  13. "tags": [
  14. "x",
  15. "y",
  16. "z"
  17. ],
  18. "cycle": "complex"
  19. }
  20. }
  21. }

Demo

huangapple
  • 本文由 发表于 2023年5月25日 20:24:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332247.html
匿名

发表评论

匿名网友

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

确定