我可以向 sonarr api 发出请求,它将给我以下输出:
curl -sL "http://192.168.2.15:8005/api/series/lookup?term=tvdb:81189&apikey=[[REMOVED]]" | jq .[]
{
"title": "Breaking Bad",
"sortTitle": "breaking bad",
"seasonCount": 5,
"status": "ended",
"overview": "When Walter White, a chemistry teacher, is diagnosed with Stage III cancer and given a prognosis of two years left to live, he chooses to enter a dangerous world of drugs and crime with the intent to secure his family's financial security.",
"network": "Netflix",
"airTime": "21:00",
"images": [
{
"coverType": "banner",
"url": "https://artworks.thetvdb.com/banners/graphical/81189-g21.jpg"
},
{
"coverType": "poster",
"url": "https://artworks.thetvdb.com/banners/posters/81189-10.jpg"
},
{
"coverType": "fanart",
"url": "https://artworks.thetvdb.com/banners/fanart/original/81189-21.jpg"
}
],
"remotePoster": "https://artworks.thetvdb.com/banners/posters/81189-10.jpg",
"seasons": [
{
"seasonNumber": 0,
"monitored": false
},
{
"seasonNumber": 1,
"monitored": true
},
{
"seasonNumber": 2,
"monitored": true
},
{
"seasonNumber": 3,
"monitored": true
},
{
"seasonNumber": 4,
"monitored": true
},
{
"seasonNumber": 5,
"monitored": true
}
],
"year": 2008,
"profileId": 0,
"languageProfileId": 0,
"seasonFolder": false,
"monitored": true,
"useSceneNumbering": false,
"runtime": 47,
"tvdbId": 81189,
"tvRageId": 18164,
"tvMazeId": 169,
"firstAired": "2008-01-20T00:00:00Z",
"seriesType": "standard",
"cleanTitle": "breakingbad",
"imdbId": "tt0903747",
"titleSlug": "breaking-bad",
"certification": "TV-MA",
"genres": [
"Crime",
"Drama",
"Suspense",
"Thriller"
],
"tags": [],
"added": "0001-01-01T00:00:00Z",
"ratings": {
"votes": 31714,
"value": 9.4
},
"qualityProfileId": 0
}
我想从该输出中获取键和值,并使用这些键和值创建新的有效 json 输出。这些是新输出中需要的键(+ 值):
title
profileId
tvdbId
titleSlug
images
(及其所有内容和子项)seasons
(及其所有内容和子项)
对于一个键,需要更改其值。新值存储在变量中:
profileId
(多变的$profile_id
)
需要向新输出添加一些内容(不在 api 的输出中):
"rootFolderPath": ".....",
(键的值存储在变量中$root_folder_path
)
"addOptions":
{
"ignoreEpisodesWithFiles": false,
"ignoreEpisodesWithoutFiles": false,
"searchForMissingEpisodes": false
}
结果(以上面的示例输出为准)应该如下:
{
"title": "Breaking Bad",
"rootFolderPath": "/home/cas/plex-media/series",
"profileId": 1,
"tvdbId": 81189,
"titleSlug": "breaking-bad",
"images": [
{
"coverType": "banner",
"url": "https://artworks.thetvdb.com/banners/graphical/81189-g21.jpg"
},
{
"coverType": "poster",
"url": "https://artworks.thetvdb.com/banners/posters/81189-10.jpg"
},
{
"coverType": "fanart",
"url": "https://artworks.thetvdb.com/banners/fanart/original/81189-21.jpg"
}
],
"seasons": [
{
"seasonNumber": 0,
"monitored": false
},
{
"seasonNumber": 1,
"monitored": true
},
{
"seasonNumber": 2,
"monitored": true
},
{
"seasonNumber": 3,
"monitored": true
},
{
"seasonNumber": 4,
"monitored": true
},
{
"seasonNumber": 5,
"monitored": true
}
],
"addOptions":
{
"ignoreEpisodesWithFiles": false,
"ignoreEpisodesWithoutFiles": false,
"searchForMissingEpisodes": false
}
}
我非常希望它能够在不保存到文件的情况下工作。因此它可以是一小段 bash 代码或一行/长管道,只要它不涉及保存到文件或类似的东西。只需完成所有需要做的事情,并将新输出安全地保存在变量中即可。
答案1
您可以jq
使用 将简单字符串传递到过滤器中--arg
,并类似地使用 JSON 对象--argjson
。然后使用{}
对象构造组装所需的输出:
profile_id=1
root_folder_path="/home/cas/plex-media/series"
add_options='
{
"ignoreEpisodesWithFiles": false,
"ignoreEpisodesWithoutFiles": false,
"searchForMissingEpisodes": false
}'
jq --argjson p "$profile_id" --argjson a "$add_options" --arg r "$root_folder_path" '
{title, rootFolderPath: $r, profileId: $p, tvdbId, titleSlug, images, seasons, addOptions: $a}
' input.json
请注意,该值$profile_id
需要作为 JSON 对象传递才能被视为数字。
也可以看看: