jq 无法使用 join 迭代数字

发布于 2025-01-09 20:52:50 字数 1354 浏览 1 评论 0原文

我想从 json 文件中添加一些由管道分隔的值。到目前为止,它运行良好,直到值是数字而不是字符串为止。

这是我到目前为止所做的:jq -r '.content[] | {季节标题、编号、名称} | join("|")' file.json

我尝试将数字转换为字符串,但没有成功jq -r '.content[] | {seasonTitle, "episodeNumber|tostring", name} | join("|")' file.json

实际结果:

Top Master||Last Chance / Season 12
Top Master||Épisode 8 / Season 12
Top Master||Épisode 7 / Season 12

预期结果:

Top Master|236|Last Chance / Season 12
Top Master|235|Épisode 8 / Season 12
Top Master|234|Épisode 7 / Season 12

这里是 file.json

{
  "page": 0,
  "size": 3,
  "count": 3,
  "content": [
    {
      "name": "Last Chance / Season 12",
      "releaseDate": "2008",
      "duration": 2100,
      "episodeNumber": 236,
      "title": "Last Chance / Season 12",
      "seasonTitle": "Top Master"
    },
    {
      "name": "Épisode 8 / Season 12",
      "releaseDate": "2008",
      "duration": 7320,
      "episodeNumber": 235,
      "title": "Épisode 8 / Season 12",
      "seasonTitle": "Top Master"
    },
    {
      "name": "Épisode 7 / Season 12",
      "releaseDate": "2008",
      "duration": 7200,
      "episodeNumber": 234,
      "title": "Épisode 7 / Season 12",
      "seasonTitle": "Top Master"
    }
  ]
}

I would like to add some values from json file separated by pipe. It's working well so far until a value is a number and not a string.

Here what I've done so far: jq -r '.content[] | {seasonTitle, number, name} | join("|")' file.json

I've tried to convert number to string without any success jq -r '.content[] | {seasonTitle, "episodeNumber|tostring", name} | join("|")' file.json

Actual Result:

Top Master||Last Chance / Season 12
Top Master||Épisode 8 / Season 12
Top Master||Épisode 7 / Season 12

Expected Result:

Top Master|236|Last Chance / Season 12
Top Master|235|Épisode 8 / Season 12
Top Master|234|Épisode 7 / Season 12

Here the file.json

{
  "page": 0,
  "size": 3,
  "count": 3,
  "content": [
    {
      "name": "Last Chance / Season 12",
      "releaseDate": "2008",
      "duration": 2100,
      "episodeNumber": 236,
      "title": "Last Chance / Season 12",
      "seasonTitle": "Top Master"
    },
    {
      "name": "Épisode 8 / Season 12",
      "releaseDate": "2008",
      "duration": 7320,
      "episodeNumber": 235,
      "title": "Épisode 8 / Season 12",
      "seasonTitle": "Top Master"
    },
    {
      "name": "Épisode 7 / Season 12",
      "releaseDate": "2008",
      "duration": 7200,
      "episodeNumber": 234,
      "title": "Épisode 7 / Season 12",
      "seasonTitle": "Top Master"
    }
  ]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2025-01-16 20:52:50

您正在使用 join 来连接不同类型的值,这在 jq v1.6 下工作正常:

.content[] | {seasonTitle, episodeNumber, name} | join("|")
Top Master|236|Last Chance / Season 12
Top Master|235|Épisode 8 / Season 12
Top Master|234|Épisode 7 / Season 12

演示

但是,对于 jq v1.5 则不然,您需要使用 tostring 将非字符串转换为字符串。当您使用快捷方式为 join 创建对象时,引入此转换会牺牲解决方案的简洁性。因此,要么坚持使用它:

.content[] | {seasonTitle, episodeNumber: (.episodeNumber | tostring), name} | join("|")

要么使用数组,因为无论如何您都只需要值:

.content[] | [.seasonTitle, (.episodeNumber | tostring), .name] | join("|")

You are using join to concatenate values of different types, which works fine under jq v1.6:

.content[] | {seasonTitle, episodeNumber, name} | join("|")
Top Master|236|Last Chance / Season 12
Top Master|235|Épisode 8 / Season 12
Top Master|234|Épisode 7 / Season 12

Demo

However, with jq v1.5 it doesn't, and you need to convert non-strings to strings using tostring. As you are using a shortcut to create an object for join, introducing this conversion sacrifices the conciseness of your solution. So either stick with it:

.content[] | {seasonTitle, episodeNumber: (.episodeNumber | tostring), name} | join("|")

Or use an array instead, as you are going for the values only anyway:

.content[] | [.seasonTitle, (.episodeNumber | tostring), .name] | join("|")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文