AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1336910
Accepted
Cas
Cas
Asked: 2021-05-09 02:41:50 +0800 CST2021-05-09 02:41:50 +0800 CST 2021-05-09 02:41:50 +0800 CST

如何获取 json 输出的键和值并使用它们创建新的有效 json?

  • 772

我可以向 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 输出。这些是需要在新输出中的键(+ 值):

  1. title
  2. profileId
  3. tvdbId
  4. titleSlug
  5. images(及其所有内容和子项)
  6. seasons(及其所有内容和子项)

对于一个键,值需要更改。新值存储在一个变量中:

  1. profileId(变量$profile_id)

需要将一些东西添加到新输出中(不在 api 的输出中):

  1. "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 代码或单线/长管道,只要它不涉及保存到文件或其他东西。例如,只需完成所有需要完成的工作并将新输出保护在变量中。

command-line
  • 1 1 个回答
  • 582 Views

1 个回答

  • Voted
  1. Best Answer
    steeldriver
    2021-05-09T05:29:36+08:002021-05-09T05:29:36+08:00

    您可以使用 将简单的字符串传递给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 对象传递才能被视为数字。

    也可以看看:

    • jq 手册:调用 jq
    • jq 手册:类型和值
    • 0

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve