{"id":25343864,"url":"https://github.com/appscode-cloud/json-filter","last_synced_at":"2025-10-08T00:06:58.776Z","repository":{"id":43881032,"uuid":"278540921","full_name":"appscode-cloud/json-filter","owner":"appscode-cloud","description":"A pakcage to filter a json object depending on an array of operations given","archived":false,"fork":false,"pushed_at":"2024-12-28T22:53:58.000Z","size":45,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-08-02T00:37:48.335Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/appscode-cloud.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-07-10T04:50:02.000Z","updated_at":"2024-12-28T22:49:28.000Z","dependencies_parsed_at":"2025-08-02T00:25:05.010Z","dependency_job_id":"ae458786-4cf4-40b2-ae4f-39b655fee340","html_url":"https://github.com/appscode-cloud/json-filter","commit_stats":null,"previous_names":["appscode/json-filter","bytebuilders/json-filter"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/appscode-cloud/json-filter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appscode-cloud%2Fjson-filter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appscode-cloud%2Fjson-filter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appscode-cloud%2Fjson-filter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appscode-cloud%2Fjson-filter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/appscode-cloud","download_url":"https://codeload.github.com/appscode-cloud/json-filter/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appscode-cloud%2Fjson-filter/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278866931,"owners_count":26059671,"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","status":"online","status_checked_at":"2025-10-07T02:00:06.786Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2025-02-14T10:59:10.532Z","updated_at":"2025-10-08T00:06:58.761Z","avatar_url":"https://github.com/appscode-cloud.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JSON-FILTER\n\nThis is a tool to filter out relevant information out of JSON objects by providing a set of operations.\n\n## Install\n\n```bash\nnpm i @appscode/json-filter\n```\n\n## Usage\n\n```js\n// import the package\nimport JsonFilter from \"@appscode/json-filter\";\n\n// call the function\nconst filteredOutput = JsonFilter(jsonObject, operations);\n```\n\n## Parameters\n\nThe filter function takes 2 parameters. One is the JSON object we want to filter or extract relevant information from. The other one is the Array of operations that we want to run in order to filter out our information.\n\n### Operations\n\nIn order to filter out relvent fields from the javascript object, you need to provide a set of operations to the filter function.\n\n- Each operation run on the aggregated output of the previous operations.\n- If the agrregated output of the previous operations is an array, then the new operation is performed on each item of the array.\n\nThere are two types of operation:\n\n1. **Fetch:** This operations fetches the value at a given path inside the object.\n\n   ```json\n   {\n     \"$ref\": \"#/demo/path\"\n   }\n   ```\n\n2. **Fetch \u0026 Map:** This operation fetches relevant values and maps them into a new object\n\n   ```json\n   {\n     \"prop1\": {\n       \"$ref\": \"#/demo/path/to/prop1\"\n     },\n     \"prop2\": {\n       \"$ref\": \"#/demo/path/to/prop2\"\n     }\n   }\n   ```\n\n## Examples\n\nExample json object:\n\n```javascript\nconst jsonOb = {\n  name: \"John Doe\",\n  age: 18,\n  previousJobs: [\n    {\n      companyName: \"Appscode\",\n      desgnation: \"Front-end Software Engineer\",\n      duration: 2,\n    },\n    {\n      companyName: \"Google\",\n      desgnation: \"Software Engineer\",\n      duration: 1,\n    },\n  ],\n};\n```\n\nNow, let's fetch the array of previous jobs\n\n```javascript\nconst operations = [\n  {\n    $ref: \"#/previousJobs\",\n  },\n];\nconst previousJobs = JsonFilter(jsonOb, operations);\nconsole.log(previousJobs);\n\n/* output\n[\n  {\n    companyName: \"Appscode\",\n    desgnation: \"Front-end Software Engineer\",\n    duration: 2\n  },\n  {\n    companyName: \"Google\",\n    desgnation: \"Software Engineer\",\n    duration: 1\n  }\n]\n*/\n```\n\nLet's say, we dont need the whole company object in our previous jobs array. We just want the array of previous company names. Then, our operatons array will look like -\n\n```javascript\nconst operations = [\n  {\n    $ref: \"#/previousJobs\",\n  },\n  {\n    $ref: \"#/companyName\",\n  },\n];\nconst previousJobs = JsonFilter(jsonOb, operations);\nconsole.log(previousJobs);\n\n/* output\n[\n  \"Appscode\",\n  \"Google\"\n]\n*/\n```\n\nNow, if we only want specific properties of the company object, then we would have to use **Fetch \u0026 Map** type operatoins. Let's suppose, we only want the `companyName` and `designation` in our company object array.\n\n```javascript\nconst operations = [\n  {\n    $ref: \"#/previousJobs\",\n  },\n  {\n    companyName: {\n      $ref: \"#/companyName\",\n    },\n    designation: {\n      $ref: \"#/designation\",\n    },\n  },\n];\nconst previousJobs = JsonFilter(jsonOb, operations);\nconsole.log(previousJobs);\n\n/* output\n[\n  {\n    companyName: \"Appscode\",\n    desgnation: \"Front-end Software Engineer\",\n  },\n  {\n    companyName: \"Google\",\n    desgnation: \"Software Engineer\",\n  }\n]\n*/\n```\n\nWe can also rename these properties to our desired names. eg:\n\n```javascript\nconst operations = [\n  {\n    $ref: \"#/previousJobs\",\n  },\n  {\n    name: {\n      $ref: \"#/companyName\",\n    },\n    position: {\n      $ref: \"#/designation\",\n    },\n  },\n];\nconst previousJobs = JsonFilter(jsonOb, operations);\nconsole.log(previousJobs);\n\n/* output\n[\n  {\n    name: \"Appscode\",\n    position: \"Front-end Software Engineer\",\n  },\n  {\n    name: \"Google\",\n    position: \"Software Engineer\",\n  }\n]\n*/\n```\n\nIf we want, we can name these properties according to values inside the obects. We have to put the relative paths to the values we want as property names. **If property name starts with `#`, it is considered to be a relative path to the actual value.** Eg: -\n\n```javascript\nconst operations = [\n  {\n    $ref: \"#/previousJobs\",\n  },\n  {\n    name: {\n      $ref: \"#/companyName\",\n    },\n    \"#/companyName\": {\n      $ref: \"#/designation\",\n    },\n  },\n];\nconst previousJobs = JsonFilter(jsonOb, operations);\nconsole.log(previousJobs);\n\n/* output\n[\n  {\n    name: \"Appscode\",\n    Appscode: \"Front-end Software Engineer\",\n  },\n  {\n    name: \"Google\",\n    Google: \"Software Engineer\",\n  }\n]\n*/\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fappscode-cloud%2Fjson-filter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fappscode-cloud%2Fjson-filter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fappscode-cloud%2Fjson-filter/lists"}