Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort a json file by keys and values of those keys in jq

Tags:

json

sorting

jq

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?

like image 446
karlos Avatar asked May 19 '15 16:05

karlos


People also ask

How do I sort a JSON key?

Enter your JSON into the first text area, or drag and drop a file, after, select the sort method you're going to use, key value requires the key name (if not specified selects the first key), click the example button to get an idea on how it works. The result will automatically sort and display in the output text area.

Can JSON be sorted?

JSON return type is an array of objects. Hence sort method cannot be used directly to sort the array. However, we can use a comparer function as the argument of the 'sort' method to get the sorting implemented.


2 Answers

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

Select the right object,
walk into arrays if needed,
then sort_by a single value.

I was trying sort_by(.components.rows.id) which failed.

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

like image 136
karlos Avatar answered Oct 22 '22 10:10

karlos


This doesn't answer the question, but here is another way to sort by attributes/keys:

jq --sort-keys . my_file > sorted_file

-or-

jq -S . my_file > sorted_file
like image 88
Thibaud Ledent Avatar answered Oct 22 '22 11:10

Thibaud Ledent