如何将JSON值密钥对传递到bash命令中

  • 本文关键字:bash 命令 密钥对 JSON bash
  • 更新时间 :
  • 英文 :


我遇到了这个问题,我想从BASH中的json字符串中读取值,并将它们传递给BASH命令。基本上我想转换,例如:

{first: a, second: b} -> first=a second=b
{first: a, second: b, third: c} -> first=a second=b third=c
{1: a, 2:b} -> 1=a, 2=b

无论json字符串中有多少键值对,我该如何做到这一点?

到目前为止,我已经走到了这一步:

a='{"first": "a", "second": "b"}'
echo ${a} | jq -r 'to_entries | .[] | .key + "=" + .value'
first=a
second=b

但我希望它在同一条线上,用">

您可以使用名为jq的工具将JSON传递到命令中,如果需要,还可以对其进行编码,并包含更多功能。

更多信息可以在以下链接中找到,https://stedolan.github.io/jq/

最新更新