Outils pour utilisateurs

Outils du site


informatique:jq

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
informatique:jq [03/10/2021 12:11] cyrilleinformatique:jq [04/10/2021 08:40] (Version actuelle) – [jq] cyrille
Ligne 1: Ligne 1:
 ====== jq ====== ====== jq ======
  
-[[https://github.com/stedolan/jq/|jq]]+**[[https://stedolan.github.io/jq/|jq]]** a lightweight and flexible command-line JSON processor.
  
  
 +  * Le [[https://stedolan.github.io/jq/manual/|Manuel]]
   * La [[https://github.com/stedolan/jq/wiki/FAQ|FAQ]]   * La [[https://github.com/stedolan/jq/wiki/FAQ|FAQ]]
   * Le [[https://github.com/stedolan/jq/wiki/Cookbook|Cookbook]]   * Le [[https://github.com/stedolan/jq/wiki/Cookbook|Cookbook]]
  
 +  * Pour faire des requêtes en ligne : https://jqplay.org
  
   * [[https://kaijento.github.io/2017/03/20/json-parsing-jq-group_by-max_by-and-sort_by/|JSON parsing: jq group_by() max_by() sort_by()]]   * [[https://kaijento.github.io/2017/03/20/json-parsing-jq-group_by-max_by-and-sort_by/|JSON parsing: jq group_by() max_by() sort_by()]]
   * [[https://gist.github.com/olih/f7437fb6962fb3ee9fe95bda8d2c8fa4|Processing JSON using jq]]   * [[https://gist.github.com/olih/f7437fb6962fb3ee9fe95bda8d2c8fa4|Processing JSON using jq]]
   * [[https://stackoverflow.com/questions/tagged/jq|jq sur stackoverflow]]    * [[https://stackoverflow.com/questions/tagged/jq|jq sur stackoverflow]] 
 +
 +
 +===== Play with jq =====
 +
 +''members.json'':
 +<code>
 +[
 + {
 +  "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"}
 +    ]
 +   }
 +  ]
 + }
 +]
 +</code>
 +
 +==== Howto count occurrence of "grouped by key=value" with jq? ====
 +
 +https://stackoverflow.com/questions/69423726/howto-count-occurrence-of-grouped-by-key-with-jq/
 +
 +=== Sans group_by ===
 +
 +<code bash>
 +jq '"foo" as $title | .[] | {
 +  id: .member_id,
 +  $title,
 +  count: [.loans[].media[] | select(.title == $title)] | length
 +}' members.json
 +</code>
 +<code>
 +{
 +  "id": 123,
 +  "title": "foo",
 +  "count": 2
 +}
 +{
 +  "id": 456,
 +  "title": "foo",
 +  "count": 1
 +}
 +</code>
 +
 +=== Avec group_by ===
 +
 +<code bash>
 +jq  'map(
 +         (.member_id) as $m
 +        | .loans[].media[]
 +        | select(.title=="foo")
 +        | {id: $m, title: .title}
 +        )
 +    |group_by(.id)[]
 +    |.[0] + { count: length }
 +' members.json
 +</code>
  
informatique/jq.1633255864.txt.gz · Dernière modification : 03/10/2021 12:11 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