Using jq, convert array of name/value pairs to object with named keys

怎甘沉沦 提交于 2019-12-02 18:08:10

问题


Given a json file in the format as :

[
  {
    "name" : "A",
    "value" : "4"
  },
  {
    "name" : "B",
    "value" : "2"
  },
  {
    "name" : "C",
    "value" : {
      "X": "Something",
      "Y": "Else"
    }
  }
]

How would I convert it to something like this using jq:

{
  "A": "4",
  "B": "2",
  "C": {
    "X": "Something",
    "Y": "Else"
  }
}

I did come close using jq 'map( { (.name): .value } ) but that still leaves each object in its separate braces instead of having them all together.


回答1:


Using your approach, simply add add to your filter:

map( { (.name): .value } ) | add


来源:https://stackoverflow.com/questions/52303570/using-jq-convert-array-of-name-value-pairs-to-object-with-named-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!