将地图转换为列表

时间:2019-06-17 21:04:50

标签: json jq

我正在尝试使用JQ将json中的对象映射转换为List ...

来源:

{
  "title": "title",
  "keyword": "keyword",
  "desc": {
    "user1": {
      "name": "kumar",
      "Duration": null,
      "Time": null,
      " Dominance": "Upper Field First"
    },
    "user2": {
      "id": "user2",
      "name": "user2",
      "Duration": null,
      "Time": null,
      " Dominance": "Upper Field First"
    }
  }
}

目标:

[
  {
    "id": "user1",
    "name": "kumar",
    "Duration": null,
    "Time": null,
    "Dominance": "Upper Field First"
  },
  {
    "id": "user2",
    "name": "user2",
    "Duration": null,
    "Time": null,
    "Dominance": "Upper Field First"
  }
]

我尝试了各种选择,例如,但无法获得我需要的精确东西。

  1. .desc | . as $in| keys[]
  2. to_entries

JQ工作区::: https://jqplay.org/s/MiJ9w1Sz5L

1 个答案:

答案 0 :(得分:1)

假设您已固定输入,以下过滤器将执行您想要的操作:

.desc
| to_entries
| map( {id: .key} + .value)

要了解这一点,只需阅读在线手册中的to_entries(请参阅)。