Let's start with an example output:
{ "title": "The Flash (2014)", "alternateTitles": [ { "title": "The Flash", "seasonNumber": -1 } ], "sortTitle": "flash 2014", "seasonCount": 7, "totalEpisodeCount": 154, "episodeCount": 142, "episodeFileCount": 0, "sizeOnDisk": 0, "status": "continuing", "overview": "After being struck by lightning, Barry Allen wakes up from his coma to discover he's been given the power of super speed, becoming the Flash, fighting crime in Central City.", "nextAiring": "2021-05-19T00:00:00Z", "previousAiring": "2021-05-12T00:00:00Z", "network": "The CW", "airTime": "20:00", "images": [ { "coverType": "banner", "url": "/MediaCover/37/banner.jpg?lastWrite=637552858236774582", "remoteUrl": "" }, { "coverType": "poster", "url": "/MediaCover/37/poster.jpg?lastWrite=637552858237654584", "remoteUrl": "" }, { "coverType": "fanart", "url": "/MediaCover/37/fanart.jpg?lastWrite=637552858239814588", "remoteUrl": "" } ], "seasons": [ { "seasonNumber": 0, "monitored": false, "statistics": { "episodeFileCount": 0, "episodeCount": 0, "totalEpisodeCount": 8, "sizeOnDisk": 0, "percentOfEpisodes": 0 } }, { "seasonNumber": 1, "monitored": true, "statistics": { "previousAiring": "2015-05-20T00:00:00Z", "episodeFileCount": 0, "episodeCount": 0, "totalEpisodeCount": 23, "sizeOnDisk": 0, "percentOfEpisodes": 0 } }, { "seasonNumber": 2, "monitored": true, "statistics": { "previousAiring": "2016-05-25T00:00:00Z", "episodeFileCount": 0, "episodeCount": 0, "totalEpisodeCount": 23, "sizeOnDisk": 0, "percentOfEpisodes": 0 } }, { "seasonNumber": 3, "monitored": true, "statistics": { "previousAiring": "2017-05-24T00:00:00Z", "episodeFileCount": 0, "episodeCount": 0, "totalEpisodeCount": 23, "sizeOnDisk": 0, "percentOfEpisodes": 0 } }, { "seasonNumber": 4, "monitored": true, "statistics": { "previousAiring": "2018-05-23T00:00:00Z", "episodeFileCount": 0, "episodeCount": 0, "totalEpisodeCount": 23, "sizeOnDisk": 0, "percentOfEpisodes": 0 } }, { "seasonNumber": 5, "monitored": true, "statistics": { "previousAiring": "2019-05-15T00:00:00Z", "episodeFileCount": 0, "episodeCount": 0, "totalEpisodeCount": 22, "sizeOnDisk": 0, "percentOfEpisodes": 0 } }, { "seasonNumber": 6, "monitored": true, "statistics": { "previousAiring": "2020-05-13T00:00:00Z", "episodeFileCount": 0, "episodeCount": 0, "totalEpisodeCount": 19, "sizeOnDisk": 0, "percentOfEpisodes": 0 } }, { "seasonNumber": 7, "monitored": true, "statistics": { "nextAiring": "2021-05-19T00:00:00Z", "previousAiring": "2021-05-12T00:00:00Z", "episodeFileCount": 0, "episodeCount": 0, "totalEpisodeCount": 13, "sizeOnDisk": 0, "percentOfEpisodes": 0 } } ], "year": 2014, "path": "/home/cas/plex-media/series/The Flash (2014)", "profileId": 6, "languageProfileId": 1, "seasonFolder": true, "monitored": false, "useSceneNumbering": false, "runtime": 42, "tvdbId": 279121, "tvRageId": 36939, "tvMazeId": 13, "firstAired": "2014-10-07T00:00:00Z", "lastInfoSync": "2021-05-16T03:08:01.309493Z", "seriesType": "standard", "cleanTitle": "theflash2014", "imdbId": "tt3107288", "titleSlug": "the-flash-2014", "certification": "TV-PG", "genres": [ "Action", "Adventure", "Drama", "Science Fiction" ], "tags": [], "added": "2021-04-29T09:37:02.970046Z", "ratings": { "votes": 6551, "value": 8.7 }, "qualityProfileId": 6, "id": 37
}Now, I want to change the value of .seasons | .[] | select(.seasonNumber==2).monitored to false. I was able to do it with the following:
# '.[] | select(.title==$TITLE)' is used to get example output
# '--argjson SEASON "$((season-1))"' will be 2 in this case (giving 3rd object, which is indeed season 2)
[api request] | jq -rM --arg TITLE "${info[$((series-1))]}" --arg SEASON "$((season-1))" '.[] | select(.title==$TITLE).seasons | .[] | select(.seasonNumber==$SEASON) | .monitored = false'
{ "seasonNumber": 2, "monitored": false, "statistics": { "previousAiring": "2016-05-25T00:00:00Z", "episodeFileCount": 0, "episodeCount": 0, "totalEpisodeCount": 23, "sizeOnDisk": 0, "percentOfEpisodes": 0 }
}However, I dont want this output. I now only get the object itself. I want to have the complete example output, with the changed value. I think the problems are the | after .seasons, but I don't know how to fix it.
My goal is the example output, but with monitored changed to false (and only for the season that I want)
EDIT: should mention that I want to get the output in the terminal directly and correctly. So not saving to a file at the end or in the middle of the process (and then using that in a new command). Multiple commands and variables are okay though.
ANSWER TO MURU:
#for simplicity I changed to values of the --arg's to actual value's
jq --arg TITLE "The Flash (2014)" --arg SEASON "2" '.[] | select(.title==$TITLE).seasons[] |= (select(.seasonNumber==$SEASON) |= (.monitored = false))'First of all it output's [api request] instead of [api request] | jq -rM --arg TITLE "The Flash (2014)" '.[] | select(.title==$TITLE)'
Second, it... doesn't change the value. Ignoring the complete api output instead of only the example output, it still shows "monitored": true, for "seasonNumber": 2,.
EDIT2:
I got it working:
jq --arg TITLE "The Flash (2014)" --argjson SEASON "4" '.[] | select(.title==$TITLE) | .seasons[] |= (select(.seasonNumber==$SEASON) |= (.monitored = false))'The difference:
select(.title==$TITLE).seasons[]->select(.title==$TITLE) | .seasons[]--arg SEASON "4"->--argjson SEASON "4"
1 Answer
What you want is essentially two updates (update the matching season object, and then update the season array with that object). So:
.[] | select(.title==$TITLE).seasons[] |= (select(.seasonNumber==$SEASON) |= (.monitored = false))Here, the inner update (|=) updates the selected season in the input array, and the outer |= updates the array with the result.