Outils pour utilisateurs

Outils du site


informatique:jq

jq

jq a lightweight and flexible command-line JSON processor.

Play with jq

members.json:

[
 {
  "member_id": 123,
  "loans":[
   {
    "date": "123",
    "media": [
     { "title": "foo" },
     { "title": "bar" }
    ]
   },
   {
    "date": "456",
    "media": [
     { "title": "foo" }
    ]
   }
  ]
 },
 {
  "member_id": 456,
  "loans":[
   {
    "date": "789",
    "media": [
     { "title": "foo"}
    ]
   }
  ]
 }
]

Howto count occurrence of "grouped by key=value" with jq?

Sans group_by

jq '"foo" as $title | .[] | {
  id: .member_id,
  $title,
  count: [.loans[].media[] | select(.title == $title)] | length
}' members.json
{
  "id": 123,
  "title": "foo",
  "count": 2
}
{
  "id": 456,
  "title": "foo",
  "count": 1
}

Avec group_by

jq  'map(
         (.member_id) as $m
        | .loans[].media[]
        | select(.title=="foo")
        | {id: $m, title: .title}
        )
    |group_by(.id)[]
    |.[0] + { count: length }
' members.json
informatique/jq.txt · Dernière modification : 04/10/2021 08:40 de cyrille

Sauf mention contraire, le contenu de ce wiki est placé sous les termes de la licence suivante : CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki