Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 524

Sorting a JSON array by an arbitrary order using jq

$
0
0

Given this JSON data in the file data.json:

[  { "id": 1, "entityType": "cat" },  { "id": 2, "entityType": "dog" },  { "id": 3, "entityType": "bird" },  { "id": 4, "entityType": "cat" }]

How would return an array sorted by some non-alphabetic arbitrary order (e.g dogs, birds and then cats)?

I've tried various permutations along the lines of:

jq --argjson order '["dog", "bird", "cat"]''. | sort_by( index($order[], .entityType) )' data.json

but without any joy.


Viewing all articles
Browse latest Browse all 524

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>