jq以不同的方式对KEY和VALUES进行排序-如何以相同的顺序枚举它们? [英] jq sorts KEY and VALUES in different way - how can I enumerate them in the same order?
问题描述
我使用如下curl命令获得JSON格式的REST输出
I get REST output in JSON format using curl command as below
使用以下方法单独获取密钥名称:
Getting KEY names alone using:
curl http://test.te:8080/testApp/app/version | jq '.version' | jq '. | keys'
输出:
"Archiver-Version",
"Build-Id",
"Build-Jdk",
"Build-Number",
"Build-Tag",
"Built-By"
使用以下方法单独获取值:
Getting VALUES alone using:
curl http://test.te.com:8080/testApp/app/version | jq '.version' | jq '.[]'
输出(请注意,值的顺序与键名的顺序不同;例如,第一个值"user@test.com"
是键"Built-By"
的值,而不是我期望的那样,第一个键,"Archiver-Version"
):
OUTPUT (Note how the order of values doesn't correspond to the order of key names; e.g., the first value, "user@test.com"
, is the value for key "Built-By"
, not, as I would expect, for the first key, "Archiver-Version"
):
"user@test.com"
"1634d38"
"sandbox"
"02-03-2014-13:41"
"testApp"
我试图将KEYS和VALUES分配给单独的数组,以便可以迭代它们并以表格式显示它们.
I am trying to assign the KEYS and VALUES to separate arrays, so that I can iterate over them and display them in table format.
但是这两个命令的排序方式不同,我不能直接分配值和键.
But these two commands are sorted in different way, I cannot assign the values and keys directly.
无论如何,是否有必要更改KEYS和VALUES的排序,以使两者相同?
Is there anyway to change the sorting of KEYS and VALUES so that both are same?
推荐答案
jq可以对键进行排序.参见 http://stedolan.github.io/jq/manual/#Invokingjq
jq has an option to sort the keys. See http://stedolan.github.io/jq/manual/#Invokingjq
--sort-keys / -S:
Output the fields of each object with the keys in sorted order.
但是,当前的jq发行版(1.3)还没有此增强功能,您需要通过其master分支中的最新代码来编译jq.请参见 http://stedolan.github.io/jq/download/,来自源在Linux或OS X上".
However the current released version (1.3) of jq doesn't have this enhancement yet, you'll need to compile jq via latest code from it's master branch. See http://stedolan.github.io/jq/download/ , the "From source on Linux or OS X" section.
有关此功能的完整历史和详细信息,请参见问题#79按名称对对象成员进行排序的选项或功能" https://github.com/stedolan/jq/issues/79
For a complete history and details of this feature, see issue #79 "Option or function to sort object members by name" https://github.com/stedolan/jq/issues/79
这篇关于jq以不同的方式对KEY和VALUES进行排序-如何以相同的顺序枚举它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!