如果我有一个像这样的数组:

[
  [
    "AppA",
    "ServiceA",
    "SecretKey",
    "topSecretKeyA"
  ],
  [
    "AppB",
    "ServiceB",
    "SecretKey",
    "topSecretKeyB"
  ],
  [
    "AppC",
    "ServiceC",
    "SecretKey",
    "topSecretKeyC"
  ]
]

如何将其转换为如下所示的对象?

{
  "AppA": {
    "ServiceA": {
      "SecretKey": "topSecretKeyA"
    }
  },
  "AppB": {
    "ServiceB": {
      "SecretKey": "topSecretKeyB"
    }
  },
  "AppC": {
    "ServiceC": {
      "SecretKey": "topSecretKeyC"
    }
  }
}

我想我需要使用REDUTE,但我不太清楚如何使用它.我已经try 了很多不同的版本:

jq 'reduce .[] as $k (null; {($k: .})'

推荐答案

使用setpath(manual)将给定路径设置为某个值:

reduce .[] as $k (null; setpath($k[:-1]; $k[-1]))
{
  "AppA": {
    "ServiceA": {
      "SecretKey": "topSecretKeyA"
    }
  },
  "AppB": {
    "ServiceB": {
      "SecretKey": "topSecretKeyB"
    }
  },
  "AppC": {
    "ServiceC": {
      "SecretKey": "topSecretKeyC"
    }
  }
}

Json相关问答推荐

Vega通孔信号中的动态梯度

将json数组反序列化为选项 struct

由于无效的UTF-8开始字节0xa0,JSON被拒绝,但编码似乎有效

如何判断响应数组是否存在以及S是否有其他内容...?

使用快速json库编写json可以消除所有缩进

JOLT转换以根据条件删除json对象

规范化JSON数据

导致此 Kotlin Retrofit2 错误的可能原因有哪些?

在 python 中循环 JSON 数组

报告重复的对象键

VSCode 为 python 文件添加标尺,但不为 C 文件添加标尺.为什么?

将 YAML 文件转换为 Python JSON 对象

如何判断字符串是否为json格式

使用 boost 属性树读取 int 数组

使用 JSON 的 javascript 深拷贝

没有默认构造函数的杰克逊第 3 方类

Retrofit2.0 得到 MalformedJsonException 而 json 似乎正确?

JSON 和 BSON 哪个更轻量级?

将 JSON 模式转换为 python 类

如何对 Javascript 对象进行排序,或将其转换为数组?