I have an api-request that gives the output in the json form (form? layout? body? how do you say that?). See here:
{
"title": "Another Life (2019)",
"alternateTitles": [
{
"title": "Another Life",
"seasonNumber": -1
}
],
"sortTitle": "another life 2019",
"seasonCount": 2,
"totalEpisodeCount": 20,
"episodeCount": 10,
"episodeFileCount": 10,
"sizeOnDisk": 2979171318,
"status": "continuing",
"overview": "Astronaut Niko Breckenridge and her young crew face unimaginable danger as they go on a high-risk mission to explore the genesis of an alien artifact.",
"previousAiring": "2019-07-25T07:00:00Z",
"network": "Netflix",
"airTime": "03:00",
"seasons": [
{
"seasonNumber": 1,
"monitored": true,
"statistics": {
"previousAiring": "2019-07-25T07:00:00Z",
"episodeFileCount": 10,
"episodeCount": 10,
"totalEpisodeCount": 10,
"sizeOnDisk": 2979171318,
"percentOfEpisodes": 100.0
}
},
{
"seasonNumber": 2,
"monitored": true,
"statistics": {
"episodeFileCount": 0,
"episodeCount": 0,
"totalEpisodeCount": 10,
"sizeOnDisk": 0,
"percentOfEpisodes": 0.0
}
}
],
"tags": [],
"added": "2020-12-02T15:01:43.942456Z",
"ratings": {
"votes": 26,
"value": 6.0
},
"qualityProfileId": 3,
"id": 24
}
I have about 20 of these outputs in a long list. This is one of them.
The problem
In the long list, I'll be grep-ing "\"title\": \"Another Life (2019)\""
, where Another Life (2019) can be any of the 20 series. In need to get the id (at the bottom of the output).
But doing grep -Eo "\"id\": [0-9]{1,4}"
wont work as I would get 20 Id's as an output.
Doing grep -Eo "\"title\": \"Another Life (2019)\".*\"id\": [0-9]{1,4}"
also doesn't work.
Doing grep -A 100 "\"title\": \"Another Life (2019)\""
and then grep-ing the id also doesn't work.
I can't seem to get it to work how I want. I'm having problems in general understanding how grabbing strings in a json body works.
If I choose "Devs", I want to get the id of the series Devs. If I choose (be it setting a variable or inserting the name somewhere in the command) "Prison Break", I want to get the id of the series Prison Break.
Thanks!
jq
to process JSON. – choroba Feb 11 '21 at 08:56()
are metacharacters... need to escape\(2009\)
– Feb 11 '21 at 11:28jq 'select(.title == "Another Life (2019)") | .id'
– steeldriver Feb 11 '21 at 11:50