Home > other >  JQ - merge two fields to an array with another field name
JQ - merge two fields to an array with another field name

Time:09-17

I'm trying to change a data structure, but I'm not able to find a clue how to do this with jq. I'm curious to learn.

Input:

[ 
  { "capital": "St. John's",
    "lat": 17.1172,
    "lon": -61.8457
  },
  { "capital": "Buenos Aires",
    "lat": -34.6051,
    "lon": -58.4004
  },
]

Desired output:

[ 
  { "capital": "St. John's",
    "lonlat": [ -61.8457, 17.1172]
  },
  { "capital": "Buenos Aires",
    "lonlat": [-58.4004, -34.6051]
  },
]

CodePudding user response:

Form the JSON with the fields clubbed into an array and remove the fields thereafter

map(.   { lonlat : [.lon, .lat] } | del(.lat, .lon))

jqplay demo

  • Related