We're building a website using the Pentaho CTools library, which has a graphical dashboard editor which writes out JSON-format files for part of the dashboard.
我们正在使用Pentaho CTools库构建一个网站,该库具有一个图形仪表板编辑器,可以为仪表板的一部分写出JSON格式的文件。
I'd like to apply a transform to these files before check-in to git in order to sort them by key and then by the value of certain keys. The purpose is to make diffs easier, since the editor has a habit of rearranging all of the json fields.
我想在签入git之前对这些文件应用转换,以便按键对它们进行排序,然后按某些键的值进行排序。目的是使diff变得更容易,因为编辑器有重新排列所有json字段的习惯。
For example, we might have something like this:
例如,我们可能会有这样的事情:
{
"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": "[]"
},
...
We are able to jq --sort-keys
(http://stedolan.github.io/jq/) to sort all of the keys, but I'm struggling to find out how to use the sort_by
function to then sort certain specific elements by the value of certain keys (so, in the example above, sorting by properties.name
for example. Any ideas?
我们能够使用jq --sort-keys(http://stedolan.github.io/jq/)对所有键进行排序,但我很难找到如何使用sort_by函数对某些特定的键进行排序元素按某些键的值(因此,在上面的示例中,按properties.name排序。例如。任何想法?
1 个解决方案
#1
38
Ok with some assistance on the IRC channel I've found an answer.
好的,在IRC频道的帮助下,我找到了答案。
Basically, it looks like this:
基本上,它看起来像这样:
> jq '.components.rows|=sort_by(.id)|.components.rows[].properties|=sort_by(.name)' file.json > out.json
> jq'.components.rows | = sort_by(.id)| .components.rows []。properties | = sort_by(.name)'file.json> out.json
So you do the select of the right object, walking into arrays if needed, and then sort_by just takes a single value (I was trying sort_by(.components.rows.id)
which failed).
所以你选择正确的对象,如果需要走进数组,然后sort_by只需要一个值(我正在尝试sort_by(.components.rows.id)失败)。
the |= instead of the | passes the values along instead of stripping them.
| =而不是|传递值而不是剥离它们。
#1
38
Ok with some assistance on the IRC channel I've found an answer.
好的,在IRC频道的帮助下,我找到了答案。
Basically, it looks like this:
基本上,它看起来像这样:
> jq '.components.rows|=sort_by(.id)|.components.rows[].properties|=sort_by(.name)' file.json > out.json
> jq'.components.rows | = sort_by(.id)| .components.rows []。properties | = sort_by(.name)'file.json> out.json
So you do the select of the right object, walking into arrays if needed, and then sort_by just takes a single value (I was trying sort_by(.components.rows.id)
which failed).
所以你选择正确的对象,如果需要走进数组,然后sort_by只需要一个值(我正在尝试sort_by(.components.rows.id)失败)。
the |= instead of the | passes the values along instead of stripping them.
| =而不是|传递值而不是剥离它们。