kar*_*los 36 git sorting json jq
我们正在使用Pentaho CTools库构建一个网站,该库具有一个图形仪表板编辑器,可以为仪表板的一部分写出JSON格式的文件.
我想在签入git之前对这些文件应用转换,以便按键对它们进行排序,然后按某些键的值进行排序.目的是使diff变得更容易,因为编辑器习惯重新排列所有json字段.
例如,我们可能会有这样的事情:
{
"components": {
"rows": [
{
"id": "CHARTS",
"name": "Charts",
"parent": "UnIqEiD",
"properties": [
{
"name": "Group",
"type": "Label",
"value": "Charts"
}
],
"type": "Label",
"typeDesc": "<i>Group</i>"
},
{
"id": "kjalajsdjf",
"meta_cdwSupport": "true",
"parent": "CHARTS",
"properties": [
{
"name": "name",
"type": "Id",
"value": "Value1"
},
{
"name": "title",
"type": "String",
"value": "Value2"
},
{
"name": "listeners",
"type": "Listeners",
"value": "[]"
},
...
Run Code Online (Sandbox Code Playgroud)
我们能够jq --sort-keys
(http://stedolan.github.io/jq/)对所有键进行排序,但我很难找到如何使用该sort_by
函数然后通过某些键的值对某些特定元素进行排序(所以,在上面的例子中,例如排序properties.name
.任何想法?
小智 53
这不能回答问题,但这是按属性/键排序的另一种方法:
jq --sort-keys . my_file > sorted_file
Run Code Online (Sandbox Code Playgroud)
-或者-
jq -S . my_file > sorted_file
Run Code Online (Sandbox Code Playgroud)
kar*_*los 50
好的,在IRC频道的帮助下,我找到了答案.
基本上,它看起来像这样:
> jq '.components.rows|=sort_by(.id)|.components.rows[].properties|=sort_by(.name)' file.json > out.json
所以你选择正确的对象,如果需要走进数组,然后sort_by只需要一个值(我正在尝试sort_by(.components.rows.id)
哪个失败).
| =而不是| 传递值而不是剥离它们.
归档时间: |
|
查看次数: |
28363 次 |
最近记录: |