简体   繁体   English

jq将键数组与值数组合并

[英]jq merge array of keys with array of values

I have a json fragment with an array of keys and a separate array of values.我有一个 json 片段,其中包含一个键数组和一个单独的值数组。 Key 1 should match up to Value 1, etc. I'm trying to reformat with jq but not having much luck.键 1 应该与值 1 匹配,等等。我正在尝试使用 jq 重新格式化,但运气不佳。

Original JSON:原装 JSON:

{
  "result": {
    "event.KeyValues{}.Key": [
      "name",
      "gender",
      "employee",
      "email"
    ],
    "event.KeyValues{}.Value": [
      "tyler",
      "male",
      "yes",
      "tyler@nowhere.com"
    ],
    "foo": "1",
    "bar": "2"
  }
}

Desired Output:所需的 Output:

{
    "name": "tyler",
    "gender": "male",
    "employee": "yes",
    "email": "tyler@nowhere.com"
}

Use transpose to pair keys and values.使用transpose来配对键和值。 Then you can make an object out of each pair and add them together to get the desired structure.然后,您可以从每对中制作一个 object 并将它们加在一起以获得所需的结构。

.result
| [."event.KeyValues{}.Key", ."event.KeyValues{}.Value"]
| transpose
| map({(.[0]): .[1]})
| add

Online demo在线演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM