PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 23, 2022

[FIXED] How to retrieve id of object that has min value of a complex nested field

 July 23, 2022     jq, json     No comments   

Issue

This is my file myfile.gz, it's a stream of objects like this:

{
    "id": "12345",
    "type": "A",
    "count": 23,
    "monday": {
      "totalMoney": {
        "categories": {
          "FOOD": 80,
          "CLOTHES": 23,
          "TRAVEL": 0
        }
      },
      "usedMoney": {
        "categories": {
          "FOOD": 70,
          "CLOTHES": 20,
          "TRAVEL": 10
        }
      },
      "remainingMoney": {
        "categories": {
          "FOOD": 40,
          "CLOTHES": 5,
          "TRAVEL": 6
        }
      }
    },
    "tuesday": {
      "totalMoney": {
        "categories": {
          "FOOD": 20,
          "CLOTHES": 43,
          "TRAVEL": 50
        }
      },
      "usedMoney": {
        "categories": {
          "FOOD": 20,
          "CLOTHES": 19,
          "TRAVEL": 10
        }
      },
      "remainingMoney": {
        "categories": {
          "FOOD": 55,
          "CLOTHES": 5,
          "TRAVEL": 69
        }
      }
    },
    "wednesday": {
      "totalMoney": {
        "categories": {
          "FOOD": 990,
          "CLOTHES": 443,
          "TRAVEL": 550
        }
      },
      "usedMoney": {
        "categories": {
          "FOOD": 220,
          "CLOTHES": 193,
          "TRAVEL": 110
        }
      },
      "remainingMoney": {
        "categories": {
          "FOOD": 525,
          "CLOTHES": 51,
          "TRAVEL": 619
        }
      }
    }, ...
}
{
    "id": "54321",
    "type": "B",
    "count": 3,
    "monday": {
      "totalMoney": {
        "categories": {
          "FOOD": 80,
          "CLOTHES": 23,
          "TRAVEL": 0
        }
      },
      "usedMoney": {
        "categories": {
          "FOOD": 70,
          "CLOTHES": 20,
          "TRAVEL": 10
        }
      },
      "remainingMoney": {
        "categories": {
          "FOOD": 40,
          "CLOTHES": 5,
          "TRAVEL": 6
        }
      }
    },
    "tuesday": {
      "totalMoney": {
        "categories": {
          "FOOD": 20,
          "CLOTHES": 43,
          "TRAVEL": 50
        }
      },
      "usedMoney": {
        "categories": {
          "FOOD": 20,
          "CLOTHES": 19,
          "TRAVEL": 10
        }
      },
      "remainingMoney": {
        "categories": {
          "FOOD": 55,
          "CLOTHES": 5,
          "TRAVEL": 69
        }
      }
    },
    "wednesday": {
      "totalMoney": {
        "categories": {
          "FOOD": 990,
          "CLOTHES": 443,
          "TRAVEL": 550
        }
      },
      "usedMoney": {
        "categories": {
          "FOOD": 220,
          "CLOTHES": 193,
          "TRAVEL": 110
        }
      },
      "remainingMoney": {
        "categories": {
          "FOOD": 525,
          "CLOTHES": 51,
          "TRAVEL": 619
        }
      }
    }, ...
} ...

I want to select and return the object ID which is of type S, count > 20, and remainingMoney for FOOD inside categories on a friday is the least among all the other objects. This is my first time using jq so I am super lost and confused on how to achieve this. Any help is appreciated. Thanks!


Solution

Use -s to turn the stream into an array, then map(select(…)) to filter by your criteria, find the minimum using min_by(…) with your other criteria, and finally output your desired field .id. Add parameter -r (next to -s) if you want the output as raw text instead of JSON strings.

jq -s '
  map(select(.type == "S" and .count > 20))
  | min_by(.friday.remainingMoney.categories.FOOD)
  | .id
'


Answered By - pmf
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing