{"id":13714322,"url":"https://github.com/teacat/jsonfilter","last_synced_at":"2025-05-07T01:33:21.446Z","repository":{"id":57628204,"uuid":"215185904","full_name":"teacat/jsonfilter","owner":"teacat","description":"🔎 Filters the JSON data with `a/b/c` syntax","archived":true,"fork":false,"pushed_at":"2021-09-09T03:30:17.000Z","size":20,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-14T01:33:23.694Z","etag":null,"topics":["filter","filtering","go","golang","json"],"latest_commit_sha":null,"homepage":"https://github.com/nemtsov/json-mask","language":"Go","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/teacat.png","metadata":{"files":{"readme":"README-tw.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-10-15T02:16:00.000Z","updated_at":"2024-09-13T11:06:55.000Z","dependencies_parsed_at":"2022-09-26T20:11:58.242Z","dependency_job_id":null,"html_url":"https://github.com/teacat/jsonfilter","commit_stats":null,"previous_names":["teacat/marika"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teacat%2Fjsonfilter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teacat%2Fjsonfilter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teacat%2Fjsonfilter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teacat%2Fjsonfilter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/teacat","download_url":"https://codeload.github.com/teacat/jsonfilter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252796054,"owners_count":21805492,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["filter","filtering","go","golang","json"],"created_at":"2024-08-02T23:01:57.016Z","updated_at":"2025-05-07T01:33:21.176Z","avatar_url":"https://github.com/teacat.png","language":"Go","funding_links":[],"categories":["Repositories"],"sub_categories":[],"readme":"# JSON Filter [台灣正體](./README-tw.md) [![GoDoc](https://godoc.org/github.com/teacat/jsonfilter?status.svg)](https://godoc.org/github.com/teacat/jsonfilter) [![Coverage Status](https://coveralls.io/repos/github/teacat/jsonfilter/badge.svg?branch=master)](https://coveralls.io/github/teacat/jsonfilter?branch=master) [![Build Status](https://app.travis-ci.com/teacat/jsonfilter.svg?branch=master)](https://app.travis-ci.com/teacat/jsonfilter) [![Go Report Card](https://goreportcard.com/badge/github.com/teacat/jsonfilter)](https://goreportcard.com/report/github.com/teacat/jsonfilter)\r\n\r\nJSON Filter 是一個為 REST API 或一般用途所設計的類 GraphQL JSON 資料過濾篩選函式庫。你能夠透過 `users(username,nickname)` 這種用法篩選 JSON 資料。\r\n\r\n## 特色\r\n\r\n-   過濾 JSON 資料。\r\n-   簡單易用。\r\n\r\n## 為什麼？\r\n\r\nGraphQL 雖然解決的資料撈取的問題，但你需要花費許多心思在設計完美的結構、系統上。而 REST 設計方式總是最簡單、快速的。即便如此，我們還是會想要盡可能地擁有類似 GraphQL 那樣的特點。\r\n\r\n在這裡不得不提 REST API 會有一些問題：過度撈取、撈取不足。例如：你可能只需要一點資料，但單個 API 卻回傳過多的複雜結構；又或者你可能需要呼叫並彙整多個 API 才能得到你想要的資料。\r\n\r\nGoogle 提出 [Partial Resources](https://cloud.google.com/compute/docs/api/how-tos/performance#partial) 解決了這部份的問題，呼叫 Google API 時可以傳入 `fields=kind,items(title,characteristics/length)` 參數過濾回應資料。\r\n\r\n而 Adobe 也做出了[類似的舉動](https://devdocs.magento.com/guides/v2.4/rest/retrieve-filtered-responses.html)但用法有點不同：`?fields=items[name,qty,sku]`。\r\n\r\n## 備註\r\n\r\n目前的實作方式是基於 [nemtsov/json-mask](https://github.com/nemtsov/json-mask) 並以 JavaScript 直譯器（由 [robertkrimen/otto](https://github.com/robertkrimen/otto) 驅動）執行，所以效能上可能需要多加顧慮。\r\n\r\n完整的 Golang 解決方案尚未完成且不會於近日推出。\r\n\r\n## 安裝方式\r\n\r\n透過 `go get` 指令安裝套件。\r\n\r\n```bash\r\n$ go get github.com/teacat/jsonfilter\r\n```\r\n\r\n## 使用方式\r\n\r\n一個完整的範例如下：\r\n\r\n```go\r\npackage main\r\n\r\nimport (\r\n\t\"fmt\"\r\n\t\"github.com/teacat/jsonfilter\"\r\n)\r\n\r\ntype User struct {\r\n\tID       int      `json:\"id\"`\r\n\tUsername string   `json:\"username\"`\r\n\tEmail    string   `json:\"email\"`\r\n\tPhones   []string `json:\"phones\"`\r\n}\r\n\r\nfunc main() {\r\n\tdata := []byte(`\r\n{\r\n    \"id\"      : 1,\r\n    \"username\": \"YamiOdymel\",\r\n    \"email\"   : \"foobar@gmail.com\",\r\n    \"phones\"  : [\"0912345678\", \"0987654321\"]\r\n}`)\r\n\r\n\tb, err := jsonfilter.Filter(data, \"username,phones\")\r\n\tif err != nil {\r\n\t\tpanic(err)\r\n\t}\r\n\r\n\tvar data MyData\r\n\tif err := json.Unmarshal(b, \u0026data); err != nil {\r\n\t\tpanic(err)\r\n\t}\r\n\r\n\tfmt.Printf(\"%+v\", data) // 輸出：{ID:0 Username:YamiOdymel Email: Phones:[0912345678 0987654321]}\r\n}\r\n```\r\n\r\n## 語法\r\n\r\n語法某方面近似於 XPath：\r\n\r\n-   `a,b,c` 透過逗號分隔能夠選擇不同的欄位。\r\n-   `a/b/c` 以路徑的方式下去選取子欄位。\r\n-   `a(b,c)` 在某個欄位中批次選取其子欄位。\r\n-   `a/*/c` 萬用符號 \\* 會選擇該階層中的所有欄位。\r\n\r\n位於同個階層欲選擇不同欄位時，請使用 `a(b,c)` 而不是 `a/b,a/c`。因為 `a/c` 會將 `a/b` 的選取結果覆蓋而不是彙整。\r\n\r\n而目前萬用符號可能會取得到非預期的結果，這是目前已知的問題：[nemtsov\r\n/\r\njson-mask](https://github.com/nemtsov/json-mask): [\\* returns extra falsey values at the level of the wildcard #11](https://github.com/nemtsov/json-mask/issues/11).\r\n\r\n## 範例\r\n\r\n接下來的範例都基於這個 JSON 資料結構。\r\n\r\n```json\r\n{\r\n    \"id\": 1,\r\n    \"username\": \"YamiOdymel\",\r\n    \"friends\": [\r\n        {\r\n            \"id\": 2,\r\n            \"username\": \"Yan-K\"\r\n        }\r\n    ],\r\n    \"information\": {\r\n        \"address\": \"R.O.C (Taiwan)\",\r\n        \"skills\": [\r\n            {\r\n                \"id\": 1,\r\n                \"type\": \"eat\"\r\n            },\r\n            {\r\n                \"id\": 2,\r\n                \"type\": \"drink\"\r\n            }\r\n        ]\r\n    }\r\n}\r\n```\r\n\r\n---\r\n\r\n```json\r\n\"id,username\"\r\n\r\n{\r\n    \"id\": 1,\r\n    \"username\": \"YamiOdymel\"\r\n}\r\n```\r\n\r\n---\r\n\r\n```json\r\n\"friends(id,username)\"\r\n\r\n{\r\n    \"friends\": [\r\n        {\r\n            \"id\": 2,\r\n            \"username\": \"Yan-K\"\r\n        }\r\n    ]\r\n}\r\n```\r\n\r\n---\r\n\r\n```json\r\n\"information/skills(type)\"\r\n\"information/skills/type\"\r\n\"information/*/type\"\r\n\r\n{\r\n    \"information\": {\r\n        \"skills\": [\r\n            {\r\n                \"type\": \"eat\"\r\n            },\r\n            {\r\n                \"type\": \"drink\"\r\n            }\r\n        ]\r\n    }\r\n}\r\n```\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteacat%2Fjsonfilter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fteacat%2Fjsonfilter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteacat%2Fjsonfilter/lists"}