1

How can I sort this JSON data?

Hello,

I am curling a web service and I get this output as JSON using curl and jq. How can I sort the list by percentage, ascending or descending?

{
 "result": {
  "binance:adabnb": {
  "price": {
    "last": 0.00724,
    "high": 0.00749,
    "low": 0.00707,
    "change": {
      "percentage": 0,
      "absolute": 0
    }
  },
  "volume": 2706796.2,
  "volumeQuote": 19759.382062
},
"binance:adabtc": {
  "price": {
    "last": 1.213e-05,
    "high": 1.234e-05,
    "low": 1.158e-05,
    "change": {
      "percentage": 0.0341006,
      "absolute": 4e-07
    }
  },
  "volume": 121339053,
  "volumeQuote": 1450.07483142
},
"binance:adaeth": {
  "price": {
    "last": 0.00035813,
    "high": 0.00036636,
    "low": 0.00033522,
    "change": {
      "percentage": 0.059399497,
      "absolute": 2.008e-05
    }
  },
  "volume": 16676148,
  "volumeQuote": 5899.18121885
},
"binance:adatusd": {
  "price": {
    "last": 0.04367,
    "high": 0.04442,
    "low": 0.04141,
    "change": {
      "percentage": 0.022237828,
      "absolute": 0.00095
    }
  },
  "volume": 13075458.7,
  "volumeQuote": 563437.821146
},
"binance:adausdt": {
  "price": {
    "last": 0.04352,
    "high": 0.04407,
    "low": 0.04101,
    "change": {
      "percentage": 0.02859844,
      "absolute": 0.00121
    }

Vesa

  • 2
    Did you try the sort_by function of jq? – dirkt Jan 16 at 11:19
  • Yes, I tried it but I couldn't find the exact formula. – Vesa Jan 17 at 0:57
  • Please edit your question with what you tried and what went wrong. – dirkt Jan 17 at 6:26
1

if you're looking to preserve the same JSON structure and sort the records (within the "result") then obviously it's impossible, because top-label "result" is a JSON object: within objects you don't have a control over the order of JSON elements. it's only possible to do with JSON arrays (arrays could be re-shuffled).

However, if you're looking just to dump the individual records like "binance:adaeth" all of them sorted by "percentage", it's achievable with jtc (an alternative unix utility to jq).

assuming your json is in file.json do this:

bash $ cat file.json | jtc -w'<percentage>l: [-3]' -r -l | sort -k10
"binance:adabnb": { "price": { "change": { "absolute": 0, "percentage": 0 }, "high": 0.00749, "last": 0.00724, "low": 0.00707 }, "volume": 2706796.2, "volumeQuote": 19759.382062 }
"binance:adatusd": { "price": { "change": { "absolute": 0.00095, "percentage": 0.022237828 }, "high": 0.04442, "last": 0.04367, "low": 0.04141 }, "volume": 13075458.7, "volumeQuote": 563437.821146 }
"binance:adabtc": { "price": { "change": { "absolute": 4e-07, "percentage": 0.0341006 }, "high": 1.234e-05, "last": 1.213e-05, "low": 1.158e-05 }, "volume": 121339053, "volumeQuote": 1450.07483142 }
"binance:adaeth": { "price": { "change": { "absolute": 2.008e-05, "percentage": 0.059399497 }, "high": 0.00036636, "last": 0.00035813, "low": 0.00033522 }, "volume": 16676148, "volumeQuote": 5899.18121885 }
bash $ 

PS. you can find my jtc utility on a github.com, or just google up by keywords jtc and json

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.