Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 657

JQ — Return first element that satisfies a predicate

$
0
0

I have a string like:

"The sky is blue."

and a JSON file like

[  {"id": 1234,"name": "Entry 1","keywords": ["orange","yellow"    ]  },   {"id": 2345,"name": "Entry 2","keywords": ["red","blue","pink"    ]  },   {"id": 3456,"name": "Entry 3","keywords": ["green","blue","black"    ]  }]

I want to return the first element which has a keyword that can be found in the string.

In this case I would like to have the element with the id 2345, because the keyword "blue" occurs in the string, and it is the first match.

Is there a one-line solution with JQ (e.g. with various pipes)? I'm not looking for a solution with loops.


Viewing all articles
Browse latest Browse all 657

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>