Quantcast
Viewing all articles
Browse latest Browse all 49

Answer by David Z for jq: Find a key in a struct in an array or use default value in jq

This jq script should do the trick:

{  id,  name: (first(.Tags[] | select(.Key == "Name").Value)? // "(none)")}| "\(.id) \(.name)"

It works similar to your pseudocode, but it doesn't proactively defend against the .Tags array being absent or empty. There's no particular reason to treat that differently than the case where it exists but doesn't contain a Name entry. This program just tries to access .Value of the Name entry, and if that doesn't work for any reason at all, it substitutes "(none)".

You can use this either by putting it in a file, or directly with

$ jq -r '{id, name: ...} | "\(.id) \(.name)"' input.json

Viewing all articles
Browse latest Browse all 49

Trending Articles



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