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

Print input element even when capture() fails to match expression

$
0
0

I'm trying to extract info on certain specific dependencies from the output of DTrack's /v1/project endpoint.

The API returns data in the format:

[{"name": "project name","classifier": "LIBRARY | APPLICATION","directDependencies": <escaped JSON string in which I need to search for "com.example.commons/dep1@0.101.0" and similar strings>}, ...]

Here's what I got so far:

jq '.[] | select(.classifier=="APPLICATION") | {      id: .name,      dep1: ((.directDependencies // "") | capture(["com\\.example\\.commons/dep1@(?<version>[0-9.]+)"])),      dep2: ((.directDependencies // "") | capture(["com\\.example\\.commons/dep2@(?<version>[0-9.]+)"])),      dep3: ((.directDependencies // "") | capture(["com\\.example\\.commons/dep3@(?<version>[0-9.]+)"])),      dep4: ((.directDependencies // "") | capture(["com\\.example\\.commons/dep4@(?<version>[0-9.]+)"]))    }'

Generated output looks fine:

{"id": "project1","dep1": {"version": "0.102.0"  },"dep2": {"version": "0.102.0"  },"dep3": {"version": "0.102.0"  },"dep4": {"version": "0.102.0"  }}... // more elements

The problem is, the output only contains those of the input elements for which all four captures matched something.

I would like the output to contain all the elements of the input list, even if some of the captures fail to match. In other words, I would expect elements in the output like:

{"id": "project1","dep1": { "version": "0.1.0" },    // dep2 didn't match    // dep3 didn't match"dep4": { "version": "0.3.0" }}

I couldn't find such an option in the docs. Any ideas how to solve this?


Viewing all articles
Browse latest Browse all 611


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