如何通过jq中的键和值来对json文件进行排序 [英] How to sort a json file by keys and values of those keys in jq

查看:160
本文介绍了如何通过jq中的键和值来对json文件进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Pentaho CTools库建立一个网站,该库有一个图形仪表板编辑器,可以为仪表板的一部分写出JSON格式的文件。

'想要在签入之前对这些文件应用转换,以便按git键排序,然后再按某些键的值排序。目的是让diff更容易,因为编辑习惯重新安排所有的json字段。

例如,我们可能有这样的一些东西:

  {
components:{
rows:[
{
id:CHARTS,
name:Charts,
parent:UnIqEiD,
properties:[
{
name:Group,
type:Label,
value:Charts
}
],
type:标签,
typeDesc:< i> Group< / i>
},
{
id:kjalajsdjf,
meta_cdwSupport:true,
parent:CHARTS,
properties:[
{
name:name,
type:Id,
value:Value1
},
{
name:title,
type:String,
value:Value2
},
$ b $ name $听众
类型$听众$ $ b $值$ $ $

..

我们可以 jq --sort-keys http://stedolan.github.io/jq/ )将所有这些键,但我很努力地找出如何使用 sort_by 函数来按特定键的值对某些特定元素进行排序(所以在上例中,通过 properties.name 对前例进行排序充足。任何想法?

解决方案

好的,在IRC频道有一些帮助,我找到了答案。 b

基本上,它看起来像这样:

> jq'.components.rows | = sort_by(.id)| .components.rows []。properties | = sort_by(.name)'file.json> out.json



因此,您需要选择正确的对象,如果需要的话可以进入数组,然后sort_by只需要一个值我正在尝试 sort_by(.components.rows.id)其中失败)。



|而不是|传递值而不是剥离它们。


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.

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.

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?

解决方案

Ok with some assistance on the IRC channel I've found an answer.

Basically, it looks like this:

> 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).

the |= instead of the | passes the values along instead of stripping them.

这篇关于如何通过jq中的键和值来对json文件进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆