Describe the JSON you want. Get exactly that.
select-json-by-json keeps only the fields your spec names and returns them as a new object or array. The spec is plain JSON shaped like the result you want, and your source is never modified.
npm install select-json-by-json
Engine v6.0, package v1.6.1, MIT licensed. Runs in Node.js and modern browsers.
Try it: tick the fields you want
Each tick adds a field to the spec. The result follows.
Source
Spec
Result
Running a built-in preview of the same rules.
The spec has three rules
true |
Keep this field from the source as-is. |
|---|---|
{ ... } |
Go inside the nested object or array and apply the sub-spec. |
| Absent | Leave the field out of the result. |
Use it
Pass the source first and the spec second. You always get a fresh projection back.
import { selectJson } from "select-json-by-json";
const result = selectJson(sales, {
DATE: true,
VOUCHERNUMBER: true,
"ALLINVENTORYENTRIES.LIST": {
STOCKITEMNAME: true,
RATE: true
}
});
// sales is untouched; result holds only the fields above
Install three ways
npm package
npm install select-json-by-json
// latest engine
import { selectJson } from "select-json-by-json";
// or pin a version
import { selectJson } from "select-json-by-json/v6";
npx: copy the engine
# copies v6 to ./select-json-by-json/ npx select-json-by-json # choose a folder or version npx select-json-by-json ./lib/engine \ --version-target=v6
Plain JavaScript files, zero dependencies.
CDN in the browser
<script type="module">
import { selectJson } from
"https://keshavsoft.github.io/select-json-by-json/docs/dist/min.js";
console.log(selectJson(data, { DATE: true }));
</script>
Also available as globalThis.ks.selectJson.
Build and test
npm test |
Runs the Node.js native test suite. |
|---|---|
npm run build |
Bundles the highest src/vN with Vite into docs/dist/vN/min.js and
docs/dist/min.js. |