Jq

De drev
Aller à : navigation, rechercher

Parseur de json


Usage

jq Pattern file

example

test.json

[
    {"name":"John", "lastName":"Doe", "id":"1"},
    {"name":"Anna", "lastName":"Smith", "id":"2"},
    {"name":"Peter","lastName":"Jones", "id":"3"}
]

Pattern minimal

Le pattern minimal est le pattern identité . pour tout selectionner

jq . test.json
[
  {
    "id": 1,
    "lastName": "Doe",
    "name": "John"
  },
  {
    "id": 2,
    "lastName": "Smith",
    "name": "Anna"
  },
  {
    "id": 3,
    "lastName": "Jones",
    "name": "Peter"
  }
]

tableau

jq .[] test.json
{
  "id": 1,
  "lastName": "Doe",
  "name": "John"
}


Élément a l'index d'un tableau

jq .[0] test.json
{
  "id": 1,
  "lastName": "Doe",
  "name": "John"
}

Propriété d'un objet

.[].name test.json
"John"
"Anna"
"Peter"

Condition

jq 'if .[].name == "John" then "This is John" else "This is not John" end' test.json
"This is John"
"This is not John"
"This is not John"

Afficher deux champs par concaténation

 jq '.[] | .name + " -> " + .id' test.json
"John -> 1"
"Anna -> 2"
"Peter -> 3"

regexp

cat test.json | jq .[] | jq 'select(.name|match("^Jo"))'
{
  "id": 1,
  "lastName": "Doe",
  "name": "John"
}