使用 JQ 多次提取值

multi extract values with JQ

我有这个 JSON 文件:

{
  "range": "Sheet!A2:B100",
  "majorDimension": "ROWS",
  "values": [
    [
      "customer1_name",
      "customer1@email.com",
      "customer1_phone",
      "customer1_city"
    ],
    [
      "customer2_name",
      "customer2@email.com",
      "customer2_phone",
      "customer2_city"
    ]
  ]
}

并且,从 shell 脚本中,我想使用 JQ 提取每个块的每个值并将它们分配给一些变量。这是预期的结果:

对于这个块:

    [
      "customer1_name",
      "customer1@email.com",
      "customer1_phone",
      "customer1_city"
    ],

结果应该是这样的:

VAR1 = "customer1_name"
VAR2 = "customer1@email.com"
VAR3 = "customer1_phone"
VAR4 = "customer1_city"

对于这个块:

    [
      "customer2_name",
      "customer2@email.com",
      "customer2_phone",
      "customer2_city"
    ],

结果应该是这样的:

VAR1 = "customer2_name"
VAR2 = "customer2@email.com"
VAR3 = "customer2_phone"
VAR4 = "customer2_city"

想法是逐块读取 JSON 文件以 get/retrieve VARS 中的所有值。

有什么意见或建议吗?

谢谢

像这样使用 tr 有点老套,但是:

$ cat input

{
  "range": "Sheet!A2:B100",
  "majorDimension": "ROWS",
  "values": [
    [
      "customer1_name",
      "customer1@email.com",
      "customer1_phone",
      "customer1_city"
    ],
    [
      "customer2_name",
      "customer2@email.com",
      "customer2_phone",
      "customer2_city"
    ]
  ]
}
$ jq -rc '.values[]' input | tr -d '["]' | while IFS=, read var1 var2 var3 var4; do echo "$var1 $var2 $var3 $var4"; done
customer1_name customer1@email.com customer1_phone customer1_city
customer2_name customer2@email.com customer2_phone customer2_city

您还可以这样做:

$ jq -rc '.values[][]' input | while read name; read email; read phone; read city; do echo "$name $email $phone $city"; done;
customer1_name customer1@email.com customer1_phone customer1_city
customer2_name customer2@email.com customer2_phone customer2_city