如何使用jq基于内部数组中的字符串值过滤对象数组

时间:2022-09-25 08:02:19

I have a json file like this:

我有一个像这样的json文件:

[{ "path": "p1" "title": "t1" "tags": ["tags1"] }, { "path": "p2" "title": "t2" "tags": ["tags1", "tag2"] }, { "path": "p3" "title": "t3" "tags": ["tags2"] } ] and I would like to filter (using jq) the value based on tags and get the title as output.

[{“path”:“p1”“title”:“t1”“tags”:[“tags1”]},{“path”:“p2”“title”:“t2”“tags”:[“tags1” ,“”tag2“]},{”path“:”p3“”title“:”t3“”tags“:[”tags2“]}]我想根据标签过滤(使用jq)值并获取标题作为输出。

For instance, I would filter all the values that have tags1(and the output would be t1 and t2).

例如,我将过滤所有具有tags1的值(输出将为t1和t2)。

How can I do that ?

我怎样才能做到这一点 ?

Thank you for your answers.

谢谢您的回答。

P.S. : I found this question : How to filter an array of objects based on values in an inner array with jq? that almost have the answer but I was not able to adapt it.

附: :我发现了这个问题:如何根据内部数组中的值使用jq过滤对象数组?几乎有答案,但我无法适应它。

1 个解决方案

#1


0  

After rectifying the JSON input, the following filter produces the output shown below:

在纠正JSON输入后,以下过滤器生成如下所示的输出:

.[] | select( .tags | index("tags1") ) | .title

Output:

输出:

"t1"
"t2"

#1


0  

After rectifying the JSON input, the following filter produces the output shown below:

在纠正JSON输入后,以下过滤器生成如下所示的输出:

.[] | select( .tags | index("tags1") ) | .title

Output:

输出:

"t1"
"t2"