Jefsetax
Jefsetax

Reputation: 31

Output object of two json files with two filters using jq

I have these two json files :

File 0.json

{
  "Feline": [
    {
      "Name": "Leo",
      "Race": "Bengal",
      "Weight": "12"
    },
    {
      "Name": "Diego",
      "Race": "Toyger",
      "Weight": "24"
    }
  ]
}

File 1.json

{
  "Feline": [
    {
      "Name": "Lynx",
      "Race": "Bengal",
      "Weight": "15"
    },
    {
      "Name": "Simba",
      "Race": "Ussuri",
      "Weight": "14"
    }
  ]
}

With jq I would like the heaviest Feline whose race is Bengal of these two json file.

So the output will be

{
  "Feline": [
    {
      "Name": "Lynx",
      "Race": "Bengal",
      "Weight": "15"
    }
  ]
}

I tried to combine --slurp and --arg and pipe in max without concrete result. If someone know how to do this I'll apreciate the help.

Upvotes: 3

Views: 63

Answers (2)

RomanPerekhrest
RomanPerekhrest

Reputation: 92874

With the following pipeline:

$ jq -s '[.[] | .Feline[] | select(.Race == "Bengal")] | max_by(.Weight) | {"Feline": [.]}' f1 f2
{
  "Feline": [
    {
      "Name": "Lynx",
      "Race": "Bengal",
      "Weight": "15"
    }
  ]
}

Upvotes: 0

oguz ismail
oguz ismail

Reputation: 50795

$ jq -n '{Feline: [
  [inputs.Feline[] | select(.Race=="Bengal")] | max_by(.Weight)
]}' file1 file2
{
  "Feline": [
    {
      "Name": "Lynx",
      "Race": "Bengal",
      "Weight": "15"
    }
  ]
}

Upvotes: 1

Related Questions