jq · lesson 24 of 32 · Tables & lookups

IN

IN(…) asks whether its input is one of several values and answers true or false, so it belongs inside select(…). IN(source; values) asks whether any value of source is among values.

Examples

Example 1Members whose role is one of several

Document

{
  "team": "Platform",
  "office": { "city": "Berlin", "floor": 3 },
  "members": [
    { "name": "Ada", "age": 36, "role": "lead", "active": true,
      "email": "[email protected]", "skills": ["rust", "sql"] },
    { "name": "Linus", "age": 28, "role": "dev", "active": true,
      "email": null, "skills": ["c", "shell"] },
    { "name": "Grace", "age": 45, "role": "dev", "active": false,
      "skills": ["cobol"] },
    { "name": "Alan", "age": 41, "role": "qa", "active": true,
      "email": "[email protected]", "skills": [] }
  ]
}

Query

.members[] | select(.role | IN("dev", "qa")) | .name
  1. select( Keep a member only if the test inside is true.
  2. .role | IN("dev", "qa") True when the role equals any of the listed values.
  3. .name Then take the name.

Result

"Linus"
"Grace"
"Alan"

Try it in the playground

Example 2Is any member in QA?

Document

{
  "team": "Platform",
  "office": { "city": "Berlin", "floor": 3 },
  "members": [
    { "name": "Ada", "age": 36, "role": "lead", "active": true,
      "email": "[email protected]", "skills": ["rust", "sql"] },
    { "name": "Linus", "age": 28, "role": "dev", "active": true,
      "email": null, "skills": ["c", "shell"] },
    { "name": "Grace", "age": 45, "role": "dev", "active": false,
      "skills": ["cobol"] },
    { "name": "Alan", "age": 41, "role": "qa", "active": true,
      "email": "[email protected]", "skills": [] }
  ]
}

Query

IN(.members[].role; "qa")
  1. .members[].role The stream of values to check: every member's role.
  2. "qa" The values to look for. The answer is true if any of the first stream is among these.

Result

true

Try it in the playground

Example 3Skills that are not on a list

Document

{
  "team": "Platform",
  "office": { "city": "Berlin", "floor": 3 },
  "members": [
    { "name": "Ada", "age": 36, "role": "lead", "active": true,
      "email": "[email protected]", "skills": ["rust", "sql"] },
    { "name": "Linus", "age": 28, "role": "dev", "active": true,
      "email": null, "skills": ["c", "shell"] },
    { "name": "Grace", "age": 45, "role": "dev", "active": false,
      "skills": ["cobol"] },
    { "name": "Alan", "age": 41, "role": "qa", "active": true,
      "email": "[email protected]", "skills": [] }
  ]
}

Query

[.members[].skills[] | select(IN("rust", "sql") | not)]
  1. .members[].skills[] Every skill of every member.
  2. IN("rust", "sql") True for a skill on the list.
  3. | not Turns it around: keep the skills that are not on the list.

Result

["c","shell","cobol"]

Try it in the playground

Good to know

  • IN compares with ==: 1 and 1.0 are equal, "1" is not.
  • Without select, IN just gives the answer: .members[] | .role | IN("dev", "qa") is a stream of true and false.

About jsonquery

Query, format and learn JSON with jq, JSONPath, JMESPath and JSON Pointer. It is the engine of the desktop app, run in your browser.

Engine
The desktop app’s, compiled to WebAssembly
Licence
MIT (the engine and the app)
Source
GitHub
Contact
The contact form (private)
Public issues
GitHub
Legal
Privacy policy

Known limitations: a file over 10 MB opens read-only and one over 512 MB does not open here; the desktop app has no such limit.