{"id":23581268,"url":"https://github.com/pichsenmeister/json-filter","last_synced_at":"2025-05-06T21:06:33.517Z","repository":{"id":40771148,"uuid":"261040813","full_name":"pichsenmeister/json-filter","owner":"pichsenmeister","description":"A simple library to parse out specific elements of a larger JSON object or JSON array based on a provided JSON filter.","archived":false,"fork":false,"pushed_at":"2023-01-06T04:55:44.000Z","size":193,"stargazers_count":11,"open_issues_count":15,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-06T21:06:27.573Z","etag":null,"topics":["filter","json","json-filter","json-mask","json-parser","json-schema"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"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/pichsenmeister.png","metadata":{"files":{"readme":"README.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":"2020-05-03T23:31:32.000Z","updated_at":"2024-10-27T16:39:37.000Z","dependencies_parsed_at":"2023-02-05T09:02:11.491Z","dependency_job_id":null,"html_url":"https://github.com/pichsenmeister/json-filter","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pichsenmeister%2Fjson-filter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pichsenmeister%2Fjson-filter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pichsenmeister%2Fjson-filter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pichsenmeister%2Fjson-filter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pichsenmeister","download_url":"https://codeload.github.com/pichsenmeister/json-filter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252769397,"owners_count":21801377,"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","json","json-filter","json-mask","json-parser","json-schema"],"created_at":"2024-12-27T00:15:16.214Z","updated_at":"2025-05-06T21:06:33.501Z","avatar_url":"https://github.com/pichsenmeister.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @barreljs/json-filter\n\n\u003ca href=\"https://david-dm.org/pichsenmeister/json-filter\"\u003e\n    \u003cimg src=\"https://david-dm.org/pichsenmeister/json-filter.svg\" alt=\"Dependency Status\" /\u003e\n\u003c/a\u003e\n\u003ca href=\"https://david-dm.org/pichsenmeister/json-filter#info=devDependencies\"\u003e\n    \u003cimg src=\"https://david-dm.org/pichsenmeister/json-filter/dev-status.svg\" alt=\"devDependency Status\" /\u003e\n\u003c/a\u003e\n\n\nA simple library to parse out specific elements of a larger JSON object or JSON array based on a provided JSON filter. This makes it easier to filter out key-value pairs on any given JSON object, even more complex filter object are supported if you feel like that (feel free to check out the [tests](test/index.test.js) on examples).\n\n* [Installation](#installation)\n* [Getting started](#getting-started)\n* [Filter](#filter)\n* [Result](#result)\n* [License](#license)\n\n## Installation\n\nInstall via npm\n\n```\nnpm install @barreljs/json-filter\n```\n\nor yarn\n\n```\nyarn add @barreljs/json-filter\n```\n\n## Getting started\n\nLet's take a very simple JSON object as example ...\n```\nconst json = {\n    actions: [\n        {\n            type: 'visit',\n            property: 'website'\n        },\n        {\n            type: 'click',\n            property: 'site_signup'\n        },\n        {\n            type: 'click',\n            property: 'site_pricing'\n        }\n    ],\n    property: 'other'\n}\n```\n\n... and define a simple filter to get all `actions` that are of `{type: 'click'}`\n```\nconst filter = {\n    type: 'click'\n}\n```\n\nYou can now easily filter the original JSON object with the given filter ✨\n\n```\nconst JsonFilter = require('@barreljs/json-filter')\n\nconst result = JsonFilter(json, filter)\nconst elements = result.all()\n```\n\nThis will match all elements from the JSON object that have a property `type` with a value of `'click'`. As a result `elements` will look like this\n\n```\n[\n    {\n        type: 'click',\n        property: 'site_signup'\n    },\n    {\n        type: 'click',\n        property: 'site_pricing'\n    }\n]\n```\n\n## Filter\n\nThe filter can be any valid JSON object (arrays are not support at this point). An empty JSON filter (`{}`) would match all elements. I don't know why you would do that, but you can ¯\\\\\\_(ツ)\\_/¯\n\n\n### RegEx\n\n`json-filter` supports RegEx for filter properties\n\n```\n{\n    property: /^site_\\w*$/\n}\n```\n\n### `$any`\n\nYou can use the keyword `$any` on any filter property to match all possible values, like strings, numbers, boolean, objects, and arrays. This might not be useful in the example shown above, but can be helpful if your JSON object gets more complex.\n\n```\n{\n    property: '$any'\n}\n```\n\n### Usage\n\n#### `JsonFilter(json, filter, trim)`\n\n##### Arguments\n\n| Argument | Required | Type | Description |\n| -------- | -------- | ---- | ----------- |\n| `json`   | yes      | JSON | Any valid JSON object or array |\n| `filter`   | yes      | Object | Any valid JSON object |\n| `trim`   | no       | bool | A flag to indicate if your results should be trimmed to properties of your filter. If `false` the result will keep the structure of the original JSON, if `true` only properties defined in the filter will be returned. Default to `false`. |\n\n#### Example \n\n```\nconst result = JsonFilter(json, filter, trim)\n```\n\nwith `trim` set to `false` the above example will match the structure of the original JSON\n\n```\n[\n    {\n        type: 'click',\n        property: 'site_signup'\n    },\n    {\n        type: 'click',\n        property: 'site_pricing'\n    }\n]\n```\n\nwith `trim` set to `true`, the result will be trimmed to the structure of the filter\n\n```\n[\n    {\n        type: 'click'\n    },\n    {\n        type: 'click'\n    }\n]\n```\n\n## Result\n\nThe result is an object with following properties:\n\n| Property  | Return Type | Description |\n| --------- | -------- | --------- |\n| `length`  | Number   | Number of matching elements |\n| `all()`   | Array    | Returns all matching elements as a JSON array. |\n| `first()` | Object   | Returns the first matching element |\n| `last()`  | Object   | Returns the last matching element |\n| `get(i)`  | Object   | Returns the matching element on given index `i` |\n\n### Methods\n\n#### `.all()`\n\nReturns all matching elements as a JSON array.\n\n```\nconst result = JsonFilter(json, filter)\nconst all = result.all() // returns element array\n```\n\n#### `.first()`\n\nReturns the first matching element. If no elements match, returns `undefined`.\n\n```\nconst result = JsonFilter(json, filter)\nconst first = result.first() // returns first element\n```\n\n#### `.last()`\n\nReturns the last matching element. If no elements match, returns `undefined`. If only one element exists, it returns the same element as `.first()`.\n\n```\nconst result = JsonFilter(json, filter)\nconst last = result.last() // returns last element\n```\n\n#### `.get(index)`\n\nReturns the matching element on given index or `undefined` if no element on that index exists. Index starts at `0`.\n\n```\nconst result = JsonFilter(json, filter)\nconst element = result.get(3) // returns 4th element\n```\n\n## License\n\nThis project is licensed under the MIT license, Copyright (c) 2020 David Pichsenmeister | [pichsenmeister.com](https://pichsenmeister.com). For more information see [LICENSE](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpichsenmeister%2Fjson-filter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpichsenmeister%2Fjson-filter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpichsenmeister%2Fjson-filter/lists"}