【问题标题】:Create object id from JSON key value using jq [duplicate]使用 jq 从 JSON 键值创建对象 ID [重复]
【发布时间】:2017-02-24 16:18:17
【问题描述】:

我正在尝试通过为给定数组中的每个对象分配一个 id 来重构我的 JSON。该 id 将通过使用现有的键值 (osm_id) 创建。

这是我的输入:

[ 
{
  "geometry" : {
    "coordinates" : [ -0.7118478, 51.0930284 ],
    "type" : "Point"
  },
  "properties" : {
    "osm_id" : "262661",
    "religion" : "christian"
  },
  "type" : "Feature"
}, 
{
  "geometry" : {
    "coordinates" : [ -0.7207513, 51.0897118 ],
    "type" : "Point"
  },
  "properties" : {
    "denomination" : "catholic",
    "osm_id" : "262662",
    "religion" : "christian"
  },
  "type" : "Feature"
}
]

这是我想要的输出:

[
"262661": {
  "geometry": {
    "coordinates": [
      -0.7118478,
      51.0930284
    ],
    "type": "Point"
  },
  "properties": {
    "osm_id": "262661",
    "religion": "christian"
  },
  "type": "Feature"
},
"262662": {
  "geometry": {
    "coordinates": [
      -0.7207513,
      51.0897118
    ],
    "type": "Point"
  },
  "properties": {
     "denomination": "catholic",
     "osm_id": "262662",
     "religion": "christian"
  },
  "type": "Feature"
}
]

我一直在尝试使用 jq 来更新数据,但我不知道如何分配顶级 ID。到目前为止我有

.[] |= {geometry, properties, type}

但是任何进一步的结果都会导致错误。

感谢任何帮助或意见。

【问题讨论】:

  • 您如何获得添加到每个对象的 osm_id?

标签: json jq


【解决方案1】:

我昨天刚回答了一个类似的问题。

jq 'map( { (.properties.osm_id|tostring): . } ) | add'

请注意,当它应该是一个对象(用于键)时,您想要的输出是一个数组 []。

(上一个问题是https://stackoverflow.com/a/42428341/7613900

【讨论】:

  • 完美!非常感谢。
猜你喜欢
  • 2019-02-13
  • 2021-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-05
相关资源
最近更新 更多