如何使用jq将数字转换为字符串? [英] How do I use jq to convert number to string?

查看:646
本文介绍了如何使用jq将数字转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下jq命令和Json:

Given the following jq command and Json:

jq '.[]|[.string,.number]|join(": ")' <<< '
[
  {
    "number": 3,
    "string": "threee"
  },
  {
    "number": 7,
    "string": "seven"
  }
]
'

我正在尝试将输出格式设置为:

I'm trying to format the output as:

three: 3
seven: 7

不幸的是,我的尝试导致以下错误:

Unfortunately, my attempt is resulting in the following error:

jq:错误:无法添加字符串和数字

jq: error: string and number cannot be added

如何将数字转换为字符串,以便两者都可以连接?

How do I convert the number to string so both can be joined?

推荐答案

jq命令具有 tostring 功能.我花了一段时间才学会通过反复试验来使用它.使用方法如下:

The jq command has the tostring function. It took me a while to learn to use it by trial and error. Here is how to use it:

jq -r '.[] | [ .string, .number|tostring ] | join(": ")' <<< '
[{ "number": 9, "string": "nine"},
 { "number": 4, "string": "four"}]
'
nine: 9
four: 4

这篇关于如何使用jq将数字转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆