Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort JSON/JavaScript tuples using command-line tools

I have a list of JavaScript tuples in a file, one per line, as such:

{ x : 12, y : -1.0, as : [ 2, 0, 0 ], str : "xxx", d : 0.041 },
{ x : 27, y : 11.4, as : [ 1, 1, 7 ], str : "yyy", d : 0.235 },
{ x : -4, y :  2.0, as : [ 7, 8, 3 ], str : "zzz", d : 0.002 },
{ x : 44, y :  5.4, as : [ 9, 4, 6 ], str : "kkk", d : 0.176 },

I would like to sort them according to the value of a given field (the d field in my example), preferably using command-line tools (this is part of a process with many steps).

If it makes any difference, we can assume that all lines have exactly the same length (I can know the start and end index of the d value), although I would prefer a solution that doesn't rely on this.

like image 502
Philippe Avatar asked Mar 30 '14 03:03

Philippe


2 Answers

If there is a one JSON per row in the input data (as shown in the question), then the @Ashley Coolman's solution does not work, as written here:

Sorting JSON by value with jq can easily be done by using the sort_by() function. The main trick with using the sort_by() function is that your JSON input must be in an array. There are different ways to do this in jq (if your data is not already like this), including using the -s or --slurp option. The --slurp option will read all JSON input into a JSON array. From this point the data can be sorted by value. You can use the .[] syntax to return all elements of the array.

It means that right solution for the data from the question is the following:

cat data.json | jq -s -c 'sort_by(.d) | .[]' >> data_sorted.json
like image 151
Jaroslav Bezděk Avatar answered Nov 11 '22 18:11

Jaroslav Bezděk


Some time has passed since this question was asked and answered.

These days, a tool-based way would be to use something like jq:

cat data.json | jq 'sort_by(.d)' >> data_sorted.json

For more info check the site:

jq is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.

-https://stedolan.github.io/jq/

If for some reason you don't like jq, there are many alternatives

  • jsonpipe
  • json
  • json:select
  • json-command
  • jsawk
  • jshon
  • json2
like image 45
Ashley Coolman Avatar answered Nov 11 '22 17:11

Ashley Coolman