Skip to content

Latest commit

 

History

History
74 lines (63 loc) · 1.63 KB

How to sort,unique output using jq.md

File metadata and controls

74 lines (63 loc) · 1.63 KB

(jump to the answer)

I have json like below:

% cat example.json
{
    "values" : [
        {
            "title": "B",
            "url": "https://B"
        },
        {
            "title": "A",
            "url": "https://A"
        }
    ]
}

I want to sort the values based on title. i.e. expected output

{
  "title": "A",
  "url": "https://A"
}
{
  "title": "B",
  "url": "https://B"
}

Tried the blow. Does not work:

% jq '.values[] | sort' example.json           
jq: error (at example.json:12): object ({"title":"B...) cannot be sorted, as it is not an array

% jq '.values[] | sort_by(.title)' example.json
jq: error (at example.json:12): Cannot index string with string "title"

A:

the query here is only about sorting records by the record's inner labels

  1. to achieve the same output as in the question:
bash $ <example.json jtc -w'[title]:<>g:[-1]'
{
   "title": "A",
   "url": "https://A"
}
{
   "title": "B",
   "url": "https://B"
}
bash $ 
  1. to sort and apply in-place modification into the source file:
bash $ jtc -w'<values>l' -pi'[title]:<>g:[-1]' -f example.json 
bash $ jtc example.json
{
   "values": [
      {
         "title": "A",
         "url": "https://A"
      },
      {
         "title": "B",
         "url": "https://B"
      }
   ]
}
bash $