I have the following JSON object.
{
"1eba648810a9": {
"ID": "1eba648810a9",
"Tag": "1.0.237"
},
"2df272728204": {
"ID": "2df272728204",
"Tag": "1.0.243"
},
"2f5a63db6634": {
"ID": "2f5a63db6634",
"Tag": "1.0.179"
},
"91a86fec59b1": {
"ID": "91a86fec59b1",
"Tag": "1.0.202"
},
"9aa95de44891": {
"ID": "9aa95de44891",
"Tag": "1.0.189"
},
"a1caaccdba45": {
"ID": "a1caaccdba45",
"Tag": "1.0.183"
},
"a35ad9d93e62": {
"ID": "a35ad9d93e62",
"Tag": "1.0.42"
},
"a4e57f2db7eb": {
"ID": "a4e57f2db7eb",
"Tag": "1.0.181"
},
"d0254d6406e0": {
"ID": "d0254d6406e0",
"Tag": "1.0.253"
},
"ddeeb3d41a4c": {
"ID": "ddeeb3d41a4c",
"Tag": "1.0.177"
},
"f92356a49d78": {
"ID": "f92356a49d78",
"Tag": "1.0.178"
}
}
How do I convert this into an array and sort it by Tag
. In the end I want something like this.
[
{
"ID": "ddeeb3d41a4c",
"Tag": "1.0.177"
},
{
"ID": "f92356a49d78",
"Tag": "1.0.178"
},
{
"ID": "2f5a63db6634",
"Tag": "1.0.179"
},
{
"ID": "a4e57f2db7eb",
"Tag": "1.0.181"
},
{
"ID": "a1caaccdba45",
"Tag": "1.0.183"
},
{
"ID": "9aa95de44891",
"Tag": "1.0.189"
},
{
"ID": "91a86fec59b1",
"Tag": "1.0.202"
},
{
"ID": "1eba648810a9",
"Tag": "1.0.237"
},
{
"ID": "2df272728204",
"Tag": "1.0.243"
},
{
"ID": "d0254d6406e0",
"Tag": "1.0.253"
},
{
"ID": "a35ad9d93e62",
"Tag": "1.0.42"
}
]
Note that the sorting by version is not entirely correct but that’s a separate issue. Ideally, I would like a solution using only jq
instead of other external tools.
The following question has been suggested as a duplicate but it’s not relevant because that is sorting an array, not an object.
2
Answers
You would convert the object into an array with to_entries and map
Sorting can then follow.
Just iterate over the (outer) object’s items using
.[]
, and collect them into a new array by surrounding with[…]
; or usemap(.)
with the same effect. The sorting can then be achieved usingsort_by(.Tag)
.