http://bigdatums.net/2016/11/29/sorting-json-by-value-with-jq/
[popexizhi:
自己的问题本来是同一个json体中的[]的每个key的value 排序,下面这个是同多个json同结构sort。不过这个还好,可以处理一下[]中的内容成多个json同结构再sort
]
SORTING JSON BY VALUE WITH JQ (COMMAND LINE JSON PROCESSOR)
jq is a lightweight command line JSON processor that is very easy to use. Sometimes it is helpful to see your data sorted by a particular field value. Luckily jq makes this easy to do. Here are some sample JSON records we will be working with in this post:
Sorting JSON by value with jq can easily be done by using the
sort_by() function. The main trick with using the sort_by() function is that your JSON input must be in an array. There are different ways to do this in jq (if your data is not already like this), including using the -s or --slurp option. The --slurp option will read all JSON input into a JSON array. From this point the data can be sorted by value. You can use the .[] syntax to return all elements of the array.Sorting by a Single Value Example
Sorting by Multiple Values Example
1
2
3
4
5
6
7
8
9
10
11
|
#!/bin/bash
$ cat sample.json | jq -s -c 'sort_by(.first_name, .last_name) | .[]'
{"first_name":"Aaliyah","last_name":"Cervantes"}
{"first_name":"Aaliyah","last_name":"Emerson"}
{"first_name":"Aaliyah","last_name":"Fletcher"}
{"first_name":"Aaliyah","last_name":"Hopkins"}
{"first_name":"Aaliyah","last_name":"Schroeder"}
{"first_name":"Aaliyah","last_name":"Simpson"}
{"first_name":"Aaron","last_name":"Bradley"}
|
没有评论:
发表评论