惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
博客园 - 三生石上(FineUI控件)
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
罗磊的独立博客
博客园_首页
量子位
雷峰网
雷峰网
GbyAI
GbyAI
小众软件
小众软件
酷 壳 – CoolShell
酷 壳 – CoolShell
D
DataBreaches.Net
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Cloudflare Blog
IT之家
IT之家
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 聂微东

alexwlchan’s notes

What is WS11 1DB? Blocking referrers with Caddy How to type a Spanish question mark (¿) on a Mac Non-overlapping type comparisons and Python type checkers Why does t.Setenv panic after t.Parallel? Use Path.glob() and Path.rglob() for typed versions of glob.glob() Curious clocks and colourful eyes Track which templates are used by Jinja2 Archeologists distinguish between “sherds” and “shards” A single command to test all my changed Go packages Disable the new message animations in WhatsApp Finding high-churn folders that bother Backblaze Always-on SSH agent forwarding with my Git pushes Managing the caption of a photo with AppleScript (but not PhotoKit) Goodhart’s and Campbell’s Law are different Notes from The Cornishman No. 176 (Spring 2026) Notes from The Cornishman No. 176 (Spring 2026) GitUp can’t diff text files larger than 8MB Home Testing the width of a page on a mobile device using Playwright Disable AirPods charging notifications Start a Caddy server in a subprocess during a Python session HOME_GET_ME_HOME is a Citymapper Shortcuts action The FileExistsError exception exposes a filename attribute The red-lined bubble snail Why can’t Python connect to example.com? Useful type hints for Python How to truncate the middle of long command output AirPlay Receiver can interfere with Flask apps What’s the main prefix in SQLite queries?
Filter a list of JSON object based on a list of tags
2026-04-29 · via alexwlchan’s notes

Use the arrays filter to remove empty values, the any(…) filter to check for set inclusion, then wrap the whole thing in square brackets.

Here’s a problem I’ve had several times recently: I have an array of JSON objects which have an array of string tags, and I want to filter for objects with matching tags. Sometimes the array of tags is null rather than an empty list.

Here’s an example:

[
  {"id": "square",    "tags": ["quadrilateral", "2d"]},
  {"id": "rectangle", "tags": ["quadrilateral", "2d"]},
  {"id": "triangle",  "tags": ["2d"]},
  {"id": "blob",      "tags": null},
  {"id": "tagless"}
]

(The field isn’t always called tags, but this general pattern is common.)

Here’s the jq filter I need:

jq '[ .[] | select(.tags | arrays and any(. == "quadrilateral")) ]'

whcih returns the following output:

[
  {
    "id": "square",
    "tags": [
      "quadrilateral",
      "2d"
    ]
  },
  {
    "id": "rectangle",
    "tags": [
      "quadrilateral",
      "2d"
    ]
  }
]

How it works

  • .[] is the array value iterator, which iterates over the objects in the array.

  • the select(…) function filters the array for matching objects.

  • .tags is an object identifier-index; it looks up the "tags" key in the object.

  • arrays is a built-in filter that filters for objects where .tags is an array, so it discards objects where tags is missing or null.

  • the any(…) filter filters for tag arrays where one of the items is equal to "quadrilateral".

  • the […] around the whole expression wraps the result in a new array. Skip them if you want one object per line.

Notably, I’m not using the contains(…) filter. Although it sounds useful, it can only test items of the same type – it can test if a string contains a substring, or if an array is a superset of another array, but it can’t test if an array contains a string.