jq filter json by dates

后端 未结 2 1383
-上瘾入骨i
-上瘾入骨i 2021-01-13 08:23

I have some json that looks like this

 {
  \"users\":[ {  
     \"id\":8734,
     \"last_login\":\"2016-10-04T06:59:40Z\"
  },
  {  
    \"id\":9376,
    \"l         


        
相关标签:
2条回答
  • 2021-01-13 08:48

    Just add the appropriate null checks.

    .users[] | select (.last_login | . == null or fromdateiso8601 > 1475625600).id 
    
    0 讨论(0)
  • 2021-01-13 09:01

    One way to protect against null values of .last_login would be as follows:

    .users[]
    | select ( .last_login // empty | fromdateiso8601 > 1475625600)
    | .id 
    

    For clarity, though, I'd add parentheses, e.g.:

    .users[]
    | select ( (.last_login // empty) | fromdateiso8601 > 1475625600)
    | .id 
    

    Or more defensively still, use ?:

    .users[]
    | select ( .last_login | fromdateiso8601? > 1475625600)
    | .id 
    

    If you want to include items for which .last_login evaluates to null, then you could use the filter that Jeff suggested, or perhaps:

     (.last_login | fromdateiso8601? > 1475625600) // true
    

    P.S. For "90 days before now, expressed in seconds since the beginning of the Unix epoch", you could use:

    def daysAgo(days):  (now | floor) - (days * 86400);
    
    0 讨论(0)
提交回复
热议问题