Matt B
Matt B

Reputation: 37

Finding highest value in a specific JSON field in bash

I am writing a bash script that curls POST an API. The response from the post has values returned in the following format:

{
  "other": "irrelevant-fields",
  "results": [
    {
      "datapoints": [
        {"timestamp": 1555977600, "value": 0},
        {"timestamp": 1555984800, "value": 15},
        {"timestamp": 1555992000, "value": 5}
      ]
    }
  ]
}

I want to extract the highest figure from the "value" columns but I am having problems writing this code in bash. I am a beginner at JSON and there are no real references I can use to filter out the strings and values I don't need as each array is the same except for the timestamp, but I don't care about the timestamp, just the highest value returned.

My current code is just a generic way to extract the largest number from a file in bash:

grep -Eo '[[:digit:]]+' | sort -n | tail -n 1

...but instead of 15, that returns 1555992000.

Upvotes: 0

Views: 769

Answers (2)

Reino
Reino

Reputation: 3423

Please process JSON with a proper JSON interpreter/parser, like Xidel.

$ cat <<EOF | xidel -s - -e '$json/max((.//datapoints)()/value)'
{
  "other": "irrelevant-fields",
  "results": [
    {
      "datapoints": [
        {"timestamp": 1555977600, "value": 0},
        {"timestamp": 1555984800, "value": 15},
        {"timestamp": 1555992000, "value": 5}
      ]
    }
  ]
}
EOF

This returns 15.

(or in full: -e '$json/max((results)()/(datapoints)()/value)')

Upvotes: 0

Yusdiel Rodriguez
Yusdiel Rodriguez

Reputation: 76

echo '
{
  "other": "irrelevant-fields",
  "results": [
    {
      "datapoints": [
        {"timestamp": 1555977600, "value": 0},
        {"timestamp": 1555984800, "value": 15},
        {"timestamp": 1555992000, "value": 5}
      ]
    }
  ]
}
' | jq '.results[].datapoints | max_by(.value)'

The output will be like this:

{
  "timestamp": 1555984800,
  "value": 15
}

For more information, see this Medium post on jq, or the program's home page at https://stedolan.github.io/jq/

Upvotes: 3

Related Questions