选择使用jq选择内部数组的几个元素。

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

select few elements of an inner array using jq

问题

我想要创建一个更简单的 JSON,但保持原始结构,只提取一个小样本。

例如,如果我有以下 JSON:

{
    "field1": [
        {
            "a": "F1A1",
            "b": "F1B1"
        },
        {
            "a": "F1A2",
            "b": "F1B2"
        },
        {
            "a": "F1A3",
            "b": "F1B3"
        },
        {
            "a": "F1A4",
            "b": "F1B4"
        }
    ],
    "field2": [
        {
            "a": "F2A1",
            "b": "F2B1"
        },
        {
            "a": "F2A2",
            "b": "F2B2"
        }
    ],
    "field3": [
        {
            "a": "F3A1",
            "b": "F3B1"
        },
        {
            "a": "F3A2",
            "b": "F3B2"
        }
    ]
}

我想要获取第一个字段的第一个数组元素。所以我期望得到这个结果:

{
    "field1": [
        {
            "a": "F1A1",
            "b": "F1B1"
        }
    ]
}

我执行了 jq "select(.field1[0])" tmp.json,但它返回原始的 JSON。

额外问题:
作为额外问题,如何提取字段 field1 和数组中满足 a=="F1A1"a=="F1A4" 条件的元素,你期望的结果是什么?:

{
    "field1": [
        {
            "a": "F1A1",
            "b": "F1B1"
        },
        {
            "a": "F1A4",
            "b": "F1B4"
        }
    ]
}
英文:

I want create a more simple json with the same original structure but with one a small sample.

As example, If I have this json:

{
    "field1": [
        {
            "a": "F1A1",
            "b": "F1B1"
        },
        {
            "a": "F1A2",
            "b": "F1B2"
        },
        {
            "a": "F1A3",
            "b": "F1B3"
        },
        {
            "a": "F1A4",
            "b": "F1B4"
        }
    ],
    "field2": [
        {
            "a": "F2A1",
            "b": "F2B1"
        },
        {
            "a": "F2A2",
            "b": "F2B2"
        }
    ],
    "field3": [
        {
            "a": "F3A1",
            "b": "F3B1"
        },
        {
            "a": "F3A2",
            "b": "F3B2"
        }
    ]

}

I want to get the first array element from the first field. So I was expecting this:

{
    "field1": [
        {
            "a": "F1A1",
            "b": "F1B1"
        }
    ],

}

I executed jq "select(.field1[0])" tmp.json but it returns the original json.

Bonus:
As bonus, how to do the same but extracting let's say field1 and elements in the array with a=="F1A1" and a=="F1A4", so will expect?:

{
    "field1": [
        {
            "a": "F1A1",
            "b": "F1B1"
        },
        {
            "a": "F1A4",
            "b": "F1B4"
        }
    ]

}

答案1

得分: 0

只返回翻译好的部分,不要有别的内容:

使用{field1}将外部对象减小到您的字段,然后将此字段映射到仅包含“first”项的数组:

jq '{field1} | map_values([first])'
{
  "field1": [
    {
      "a": "F1A1",
      "b": "F1B1"
    }
  ]
}

要筛选特定项,请使用select

jq '{field1} | map_values(map(select(.a == "F1A1" or .a == "F1A4")))'
{
  "field1": [
    {
      "a": "F1A1",
      "b": "F1B1"
    },
    {
      "a": "F1A4",
      "b": "F1B4"
    }
  ]
}

如您所见,select执行了不同的操作。如果参数计算为“true”,它会传递其输入。因此,其输出要么全部,要么什么都没有,永远不是仅过滤部分。当然,您可以使用select来实现特定的筛选,如上所示。

英文:

reduce the oouter object to your field using {field1}, then map this field to an array containing only the first item:

jq '{field1} | map_values([first])'
{
  "field1": [
    {
      "a": "F1A1",
      "b": "F1B1"
    }
  ]
}

To filter for certain items use select:

jq '{field1} | map_values(map(select(.a == "F1A1" or .a == "F1A4")))'
{
  "field1": [
    {
      "a": "F1A1",
      "b": "F1B1"
    },
    {
      "a": "F1A4",
      "b": "F1B4"
    }
  ]
}

As you can see, select does something different. It passes on its input if the argument evaluates to true. Therefore its output is either all or nothing, never just a filtered part. (Of course, you can use select to achieve specific filtering, as shown above.)

huangapple
  • 本文由 发表于 2023年2月19日 01:37:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495172.html
匿名

发表评论

匿名网友

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

确定