Skip to content

anttiviljami/glob-json-path

Repository files navigation

glob-json-path

CI npm version bundle size License Buy me a coffee

Bash-like globbing for JSON objects.

npm i glob-json-path
import { globValues, globPaths } from "glob-json-path";

globValues("a.b", { a: { b: 1 } }); // [1]
globValues("a.b.c", { a: { b: { c: 1 }}}); // [1]
globValues("a.b", { a: { b: { c: 1 }}}); // [{ c: 1 }]
globValues("a.*", { a: { b: 2, c: 3 }}}); // [2, 3]
globValues("a.*.c", { a: [{ b: 3, c: 4 }, { c: 5 }] }); // [4, 5]
globValues("**.c", { a: { c: 6, b: { c: 7 }}}); // [6, 7]
globValues("a*", { ab: 7, abc: 8, ba: 9, bc: 10 }); // [7, 8]
globValues("b?", { ab: 7, abc: 8, ba: 9, bc: 10 }); // [9, 10]

globPaths("a.*", { a: { b: 2, c: 3 }}); // ["a.b", "a.c"]
globPaths("a.*.c", { a: { b: { c: 3 }, { d: { c: 3 }}}}); // ["a.b.c", "a.d.c"]
globPaths("a.*.c", { a: [{ b: 3, c: 4 }, { c: 5 }] }); // ["a.0.c", "a.1.c"]
globPaths("**.c", { a: { c: 6, b: { c: 7 } } }); // ["a.c", "a.b.c"]
globPaths("a*", { ab: 7, abc: 8, ba: 9, bc: 10 }); // ["ab", "abc"]
globPaths("b?", { ab: 7, abc: 8, ba: 9, bc: 10 }); // ["ba", "bc"]

Features

  • Minimum number of dependencies. Tiny package size.
  • Return matching values or paths for bash-like globs
  • Support for wildcards *, ?
  • Support for double wildcards ** (globstar)
  • Support for arrays and all JSON primitives
  • Relies on minimatch package for converting globs to regex.

Examples

See tests for more use cases.

Contributing

glob-json-path is Free and Open Source Software. Issues and pull requests are more than welcome!